#################
### Chapter 2 ###
#################

### Figure 2.3
# --- What is the probability of observing n wars in 70 years given p=0.02

p.this.many.wars <- rep(NA, 8)

for(i in 0:7){
	p.this.many.wars[i+1] <- dbinom(i, 70, 0.02)
	}

barplot(p.this.many.wars, names.arg=c(0:7), ylab="Probability of observing...", xlab="...this many systemic wars since 1945 if we average two per century", col="red3", border=NA, cex.lab=0.8, cex.axis=0.8)

### Figure 2.4
# If the underlying probability of war is p, what is the probability of seeing n continuous periods of peace?

# Two ways to think about it. What's the probability of seeing it?, and, What's the probability that your observation comes from a world in which p is different (i.e., what are the confidence intervals on the underlying p(war))? The first just involves multiplication; the second involves a binomial test.

p.zero.wars <- rep(NA, 250)
p.one.war <- rep(NA, 250)
p.two.wars <- rep(NA, 250)
p.three.wars <- rep(NA, 250)

period <- 1:250

for(i in 1:250){
	p.zero.wars[i] <- dbinom(0, i, 0.02)	
	p.one.war[i] <- dbinom(1, i, 0.02)
	p.two.wars[i] <- dbinom(2, i, 0.02)
	p.three.wars[i] <- dbinom(3, i, 0.02)
}

library(RColorBrewer)

war.color<-brewer.pal(3,'Dark2')

plot(period, p.zero.wars, type="l", lwd=2, col=war.color[1], ylim=c(0,1), ylab="Probability of observing...", xlab="...over this many years if systemic war breaks out twice per century", cex.lab=0.8, cex.axis=0.8)
lines(period, p.one.war, lwd=2, col=war.color[2])
lines(period, p.two.wars, lwd=2, col=war.color[3])
legend("topright", c("no wars", "one war", "two wars"), lty=rep(1,3), col=war.color, lwd=2, cex=0.8)

### Figure 2.5
# --- Confidence intervals around p(war) given no war for n years

period <- rep(NA, 250)
p.same.world <- rep(NA, 250)
ci.hi <- rep(NA, 250)
ci.lo <- rep(NA, 250)

for(i in 1:250){
	period[i]<-i
	foo <- binom.test(0, i, p=0.02, alternative="less")
	p.same.world[i] <- foo$estimate
	ci.hi[i] <- foo$conf.int[2]
	ci.lo[i] <- foo$conf.int[1]
}

plot(period, rep(NA,250), type="l", col="grey50", ylim=c(0,0.25), xlim=c(0,200), ylab="Credible range of estimates of Pr(war)...", xlab="...given this many years of peace", cex.lab=0.8, cex.axis=0.8)
polygon(c(period, rev(period)), c(ci.hi, rev(ci.lo)), col="gray",border=NA)
abline(h=0.02, col="white")
text(25, 0.028, "Historical average", cex=.7, col="white")
abline(h=0.26) #Clean up axes
abline(v=208)

### Figure 2.6
WVS <- read.csv("datasets/WVS-war.csv")

GMFUS <- read.csv("datasets/GMFUS-war.csv")

WVS$Country <- as.character(WVS$Country)
WVS[52,] <- c("France", 30, 1)
WVS[53,] <- c("U.K.", 64, 1)
WVS[54,] <- c("Italy", 25, 1)
WVS[55,] <- c("Portugal", 28, 1)
WVS[56,] <- c("Slovakia", 26, 1)
WVS[57,] <- c("Bulgaria", 29, 0)

WVS$Value <- as.numeric(WVS$Value)
WVS$OECD <- as.numeric(WVS$OECD)
mean(WVS$Value[WVS$OECD==0])
mean(WVS$Value[WVS$OECD==1])

# Is the difference between OECD and non-OECD countries statistically significant?
t.test(WVS$Value ~ WVS$OECD)
# No.

sparkadd <- function(baseline, scale, series, abbr){
	mtext(abbr, side=2, at=(baseline+(.50/scale)), las=1, cex=0.6)
	rect(-1, (baseline+(.33/scale)), 1, (baseline+(.66/scale)), col="grey85", border=NA)
	lines(seq(-1, 1, length.out=11), baseline+((.01*series)/scale))
	}

quartz("Is-war-necessary-spark.pdf", 6, 9)
par(mar=c(2,1,2,1))
layout(matrix(c(1,2), 1, 2, byrow = TRUE), widths=c(4,1), heights=c(1,1))
WVS.sort <- WVS[order(WVS$Value),]
dotcolor <- rep("grey70", length(WVS.sort$Value))
dotcolor[WVS.sort$OECD==1] <- "black"
dotchart(WVS.sort$Value, labels=WVS.sort$Country, color=dotcolor, cex=0.6, pch=19, lcolor="grey95", main="Under some conditions, war is necessary to obtain justice", xlab="Percentage expressing agreement")
plot(0,0, axes="false", xlab=" ", ylab=" ", col="white") # Set up rhs graph
for(i in 1:14){
	sparkadd(GMFUS$height[i], 10, GMFUS[i, 2:12], GMFUS$A2code[i])
	}
text(-0.82,-1.05, "2003", cex=0.5)
text(0.82,-1.05, "2013", cex=0.5)

### Figure 2.8
# Source: Centers for Disease Control and Prevention, National Center for Health Statistics. Underlying Cause of Death 1999-2011 on CDC WONDER Online Database, released 2014. Data are from the Multiple Cause of Death Files, 1999-2011, as compiled from data provided by the 57 vital statistics jurisdictions through the Vital Statistics Cooperative Program. Accessed at http://wonder.cdc.gov/ucd-icd10.html on Oct 30, 2014

county <- read.csv("datasets/Poison-county.csv")
state <- read.csv("datasets/Poison-state.csv")
census <- read.csv("datasets/Poison-census.csv")

county.samp <- numeric()
county.name <- character()
for(i in 0:26){
	county.samp <- c(county.samp, county$Rate.100k[1+i*20])
	county.name <- c(county.name, as.vector(county$County)[1+i*20])
}

# The idea here is to sample at random with probability inversely proportional to rank.
# This reflects the idea that social scientists focus on the more extreme cases.
# If you're doing a case study of poisoning, you're not going to pick a place where it's rare.

set.seed(123)
county.sampnum <- sort(sample(1:875, 27, prob=1/(1:875)))
county.samp <- county$Rate.100k[county.sampnum]
county.name <- as.vector(county$County)[county.sampnum]

state <- state[order(state$Rate.100k),]

state.sampnum <- sort(sample(51:1, 9, prob=1/(1:51)))
state.samp <- state$Rate.100k[state.sampnum]
state.name <- as.vector(state$State)[state.sampnum]

barvec <- c(state.samp, 0, rev(county.samp))

par(mar=c(2,10,5,2))
par(las=2)
barplot(barvec, horiz=TRUE, col=c(rep("black", 10), rep("grey65", 27)), border=NA, space=0.5, axes=FALSE, names.arg=c(state.name, " ", rev(county.name)), cex.names=0.6)
par(las=0)
axis(3, at=c(-5, 0, 20, 40, 60, 80, 100, 120, 140), labels=c(NA, 0, 20, 40, 60, 80, 100, 120, 140), cex.axis=0.6)
mtext("Poisoning Deaths per 100,000 People per Year", side=3, line=3, cex=0.8)
axis(2, at=c(0,60), labels=NA, tck=0)

text(80,35, "County", cex=0.8)
text(80,7, "State", cex=0.8)

### Figure 2.9
data <- read.csv("datasets/WSJ.csv", header=FALSE)
colnames(data) <- c("year", "deaths")
data$year <- round(data$year)


data1 <- c(data[1:29,])
# data1<-rbind(data1, data.frame(year=2008,deaths=0))
data2 <- data[30:61,]
# data2<-rbind(data.frame(year=1946,deaths=0), data2)
# data2<-rbind(data2, data.frame(year=2008,deaths=0))
data3 <- data[62:120,]
data4 <- data[121:157,]

missing <- setdiff(1946:2008,data1$year)
data1 <- rbind(data1,data.frame(year=missing,deaths=0))
data1 <- data1[order(data1$year),]

missing <- setdiff(1946:2008,data2$year)
data2 <- rbind(data2,data.frame(year=missing,deaths=0))
data2 <- data2[order(data2$year),]

missing <- setdiff(1946:2008,data3$year)
data3 <- rbind(data3,data.frame(year=missing,deaths=0))
data3 <- data3[order(data3$year),]

missing <- setdiff(1946:2008,data4$year)
data4 <- rbind(data4,data.frame(year=missing,deaths=0))
data4 <- data4[order(data4$year),]

par(mar=c(2,4,1,1))

plot(NA, xlim=c(1946, 2008), ylim=c(0,25), ylab="Worldwide Battle Deaths per 100,000 Population", cex.axis=0.8)

for(i in 1:63){
	rect(data1[i,1]-0.4, 0, data1[i,1]+0.4, data1[i,2], col="black", border="white")
}

for(i in 1:63){
	if(data2[i,2] > data1[i,2]){
	rect(data2[i,1]-0.4, data1[i,2], data2[i,1]+0.4, data2[i,2], col="#fc8d62", border="white")
	}
}

for(i in 1:63){
	if((data3[i,2] > data1[i,2]) & (data3[i,2] > data2[i,2])){
	rect(data3[i,1]-0.4, max(data2[i,2], data1[i,2]), data3[i,1]+0.4, data3[i,2], col="#8da0cb", border="white")}
}

for(i in 1:63){
	if((data4[i,2] > data1[i,2]) & (data4[i,2] > data2[i,2]) & (data4[i,2] > data3[i,2])){
	rect(data4[i,1]-0.4, max(data3[i,2], data2[i,2], data1[i,2]), data4[i,1]+0.4, data4[i,2], col="#66c2a5", border="white")}
}

legend("topright", legend=c("Internationalized civil", "Civil", "Interstate", "Extrastate"), fill=c("#66c2a5", "#8da0cb", "#fc8d62", "black"), border="white", cex=0.8, bty="n")




### Figure 2.10
kv <- read.csv(file="datasets/PRIO_bd3.0-subset.csv")[c(1:15),c(1,2,5)]

sum(kv$bdeadbes[c(1:4)])
sum(kv$bdeadbes[c(5:15)])

zeros <- data.frame(matrix(c(rep(1,16), c(1954:1964, 1976:1980), rep(0,16)), ncol=3))
colnames(zeros) <- c("id", "year", "bdeadbes")

kv <- rbind(kv,zeros)
kv$bdeadbes <- kv$bdeadbes/1000
kv <- kv[order(kv$year),]

barplot(kv$bdeadbes, names.arg=c("1950", rep(" ",9), "1960", rep(" ",9), "1970", rep(" ",9), "1980"), axisnames=TRUE, border=NA, col=c(rep("#fc8d62", 4), rep("white", 11),  rep("#fc8d62", 11),  rep("white", 11)), xlab="Year", ylab="Battle deaths (in thousands)")
axis(1, at=c(1,37), labels=NA, tick=c(1, 12, 24, 37))
text(2, 400, "Korean War: 995,000 battle deaths", cex=0.7, pos=4)
text(20, 275, "Vietnam War: 1,461,050 battle deaths", cex=0.7, pos=4)


#################
### Chapter 3 ###
#################

rm(list=ls())

### Figure 3.1
load("datasets/ACS-PUMS.Rdata")

summary(ACS.PUMS)
attach(ACS.PUMS)

plot(sort(adj.income, decreasing=TRUE), c(1:length(adj.income)), ylab="Household Rank", xlab="Household Income", pch=19, col="#88888844", xaxt="n")
axis(1, at=c(0, 500000, 1000000, 1500000, 2000000), labels=c("$0", "$500,000", "$1,000,000", "$1,500,000", "$2,000,000"))


### Figure 3.2
options("scipen"=999)
set.seed(418)
my.samp <- sample(adj.income, 50)
ab <- lm(my.samp ~ c(1:50))$coefficients
plot(my.samp, ylab="Household Income", xlab="Respondent Number", pch=19, col="#88888888", xaxt="n", yaxt="n")
axis(1, at=c(0, 10, 20, 30, 40, 50))
axis(2, at=c(0, 200000, 400000, 600000, 800000), labels=c("$0", "$200,000", "$400,000", "$600,000", "$800,000"))
abline(a=ab[1], b=ab[2], lty=3)
ab[1]
ab[1]+50*ab[2]
# abline(h=mean(adj.income), lty=3)
legend("topright", lty=3, legend="Illusory trend")

detach(ACS.PUMS)

### Figure 3.3
# Note: I didn't set a seed when I created this Figure---I just used one of the first images that I created, so actual runs will differ from the printed figure. But the general point will hold, as you can see by running the code again and again.
library(poweRlaw)

fake.pl.series <- rpldis(1000, xmin=19.355, alpha=2.8)
19.355*(2.8-1/(2.8-1-1))  # Mean of distribution
fake.normal.series <- rnorm(1000, mean=5, sd=8)
rmean.pl <- cumsum(fake.pl.series)/c(1:1000)
rmean.norm <- cumsum(fake.normal.series)/c(1:1000)

par(mfrow=c(2,1))
plot(rmean.norm, type="l", xlab="Sample size", ylab="Normal sample mean")
abline(h=5, lty=2, col="grey50")
plot(rmean.pl, type="l", xlab="Sample size", ylab="Power law sample mean", ylim=c(min(25,min(rmean.pl)),max(rmean.pl)))
abline(h=30, lty=2, col="grey50")

#################
### Chapter 4 ###
#################

rm(list=ls())
library(ecp)

# Set up a function to draw median lines based on ecp object

draw.median.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], median(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], median(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}
	
calc.medians <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, median(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

draw.mean.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], mean(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], mean(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}

calc.means <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, mean(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}


### Figure 4.1

# Conflict initiations, PRIO data, 1945-2014

load(file="datasets/Annual-prio-international.RData")  # Annual count of 25-or-more-death conflicts from UCDP/PRIO Armed Conflict Dataset Version 4-2014a
annual <- annual.prio.international 

prio.BC.series <- matrix((annual$conflicts/annual$relevance.BC.sum), ncol=1)
prio.BC.test <- e.divisive(prio.BC.series, min.size=10, R=1000)
prio.BC.test

plot(annual$Year, prio.BC.series, type="l", col="grey80", xlab="Year", ylab=" ", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Rate of International Conflict Initiation, 1946-2014")
draw.median.lines(annual$Year, prio.BC.test$estimates, prio.BC.series)
mtext("International conflicts initiated per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1950, 1960, 1970, 1980, 1990, 2000, 2010), labels=c(1950, 1960, 1970, 1980, 1990, 2000, 2010), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

rm(list=ls())
load(file="datasets/Annual.RData")
# Annual count of reciprocated MIDs level 4 or 5, from MID-B 4.0 data
# Normalized by political relevance using data generated by EUGene (http://www.eugenesoftware.org/)
library(ecp)

# Set up a function to draw median lines based on ecp object

draw.median.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], median(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], median(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}
	
calc.medians <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, median(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

draw.mean.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], mean(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], mean(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}

calc.means <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, mean(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

### Figure 4.2

mid.BC.series <- matrix((annual$force.sum/annual$relevance.BC.sum), ncol=1)
mid.BC.test <- e.divisive(mid.BC.series, min.size=10, R=1000)
mid.BC.test

plot(annual$year, mid.BC.series, type="l", col="grey80", xlab="Year", ylab=" ", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Rate of International Conflict Initiation, 1816-2010")
draw.median.lines(annual$year, mid.BC.test$estimates, mid.BC.series)
mtext("International conflicts per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

### Figure 4.3

plot(annual$year, mid.BC.series, ylim=c(0,0.10), type="l", col="grey80", xlab="Year", ylab=" ", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Rate of International Conflict Initiation, 1816-2010")
draw.median.lines(annual$year, mid.BC.test$estimates, mid.BC.series)
mtext("International conflicts per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

### Figure 4.4

war.BC.series <- matrix((annual$war.sum/annual$relevance.BC.sum), ncol=1) # Reciprocated level-5 MIDs from same data
war.BC.test <- e.divisive(war.BC.series, min.size=10, R=1000)
war.BC.test

plot(annual$year, war.BC.series, type="l", col="grey80", xlab="Year", ylab=" ", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Rate of International War Initiation, 1816-2007")
draw.median.lines(annual$year, war.BC.test$estimates, war.BC.series)
mtext("Interstate wars per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

### Figure 4.5

plot(annual$year, war.BC.series, type="l", col="grey80", xlab="Year", ylab=" ", ylim=c(0,0.08), xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Rate of International War Initiation (Mean Lines)")
draw.mean.lines(annual$year, war.BC.test$estimates, war.BC.series)
mtext("Interstate wars per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

rm(list=ls())  # Clear out everything to avoid naming confusion

draw.median.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], median(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], median(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}
	
calc.medians <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, median(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

draw.mean.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], mean(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], mean(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}

calc.means <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, mean(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}


load(file="datasets/Annual-recip-unrecip.RData")  # MID 4.0 data, annualized, including unreciprocated level 4-5 MIDs

mid.BC.series <- matrix((annual$force.sum/annual$relevance.BC.sum), ncol=1)
mid.BC.test <- e.divisive(mid.BC.series, min.size=10, R=1000)
mid.BC.test

### Figure 4.6

plot(annual$year, mid.BC.series, ylim=c(0,0.20), type="l", col="grey80", xlab="Year", ylab=" ", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Rate of International Use of Force, 1816-2010")
draw.median.lines(annual$year, mid.BC.test$estimates, mid.BC.series)
mtext("Uses of force per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

# The next figure is a plot of FREQENCY, not rate, of war initiation over time,
# using international, extrasystemic, and non-state war data from COW.
# The "styears" dataset is simply the start years from all three datasets.

rm(list=ls())

styears <- read.csv("datasets/startyears.csv", header=FALSE)
colnames(styears) <- "year"

foo <- data.frame(table(styears$year))
foo[,1] <- as.numeric(levels(foo[,1]))[foo[,1]]
colnames(foo) <- c("year", "freq")
yrs <- data.frame(1816:2004, rep(0,189))
colnames(yrs) <- c("year", "freq")
series <- merge(foo, yrs, by="year", all.y=TRUE)
series$freq.x[is.na(series$freq.x)] <- 0
series <- series[,c(1:2)]

library(ecp)

# Set up a function to draw median lines based on ecp object

draw.median.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], median(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], median(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}
	
draw.mean.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], mean(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], mean(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}

calc.medians <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, median(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

calc.means <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, mean(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

all.war.series <- matrix((series$freq.x), ncol=1)
all.war.test <- e.divisive(all.war.series, min.size=10, R=1000)
all.war.test

### Figure 4.7

plot(series$year, all.war.series, type="l", col="grey60", xlab="Year", ylab=" ", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="All Wars")
draw.median.lines(series$year, all.war.test$estimates, all.war.series)
mtext("Frequency of wars", side=2, line=3, cex=0.9)
axis(side=1, cex.axis=0.9)
axis(side=2, cex.axis=0.9)

### Appendix Figures

### Reproduction of "mountain graph" from Pinker and Mueller, using PRIO data

rm(list=ls())
load("datasets/ucdp-prio-acd-181") 
library(doBy)
library(tidyr)
acd$conflict <- 1

foo <- summaryBy(conflict ~ year + type_of_conflict, data=acd, FUN=sum)
data <- as.data.frame(complete(foo, year, type_of_conflict))
data <- data[data$year<2010,]
data$conflict.sum[is.na(data$conflict.sum)] <- 0

# extrastate, interstate, internationalized civil, civil: 1, 2, 4, 3
colnames(data) <- c("year", "type", "wars")
data1 <- data[data$type==1,]
data2 <- data[data$type==2,]
data3 <- data[data$type==4,]
data4 <- data[data$type==3,]

# Figure A1
par(mar=c(2,4,1,1))
plot(NA, xlim=c(1946, 2010), ylim=c(0,60), xlab=" ", ylab="Number of Conflicts", cex.axis=0.8)
polygon(c(data1$year, rev(data1$year)), c(data1$wars, rep(0, length(data1$wars))), border="black", col="grey60")
polygon(c(data2$year, rev(data2$year), 1946), c((data2$wars+data1$wars), rev(data1$wars), 7), border="black", col="black")
polygon(c(data3$year, rev(data3$year), 1946), c((data3$wars+data2$wars+data1$wars), rev(data2$wars+data1$wars), 9), border="black", col="grey80")
polygon(c(data4$year, rev(data4$year), 1946), c((data4$wars+data3$wars+data2$wars+data1$wars), rev(data3$wars+data2$wars+data1$wars), 17), border="black", col="grey40")

legend("topleft", legend=c("Extrastate", "Interstate", "Internationalized Civil", "Civil"), fill=c("grey60", "black", "grey80", "grey40"), border="white", cex=0.8, bty="n")


rm(list=ls())

civwar <- read.csv("datasets/ts_onset.csv")
firstwar <- civwar$first_inst_sum
recurrence <- civwar$recur_inst_sum
firstonset <- civwar$first_ons_sum
recuronset <- civwar$recur_ons_sum
Year <- civwar$year

cwplot <- data.frame(firstwar, recurrence, firstonset, recuronset, Year)

attach(cwplot)

library(RColorBrewer)

my.Blue <- brewer.pal(4, "Blues")
my.Red <- brewer.pal(4, "Reds")

pgon <- function(x,y,z,c,b){
	polygon(c(x, rev(x)), c(y, rev(z)), col=c, border=b)
}

## Figure A2

plot(Year, firstwar+recurrence,  type="l", lty=0, ylab="Number")
pgon(Year, firstwar+recurrence, firstwar, my.Red[1], "white")
pgon(Year, firstwar+recuronset, firstwar, my.Red[4], "white")
pgon(Year, firstwar, rep(0,length(firstwar)), my.Blue[2], "white")
pgon(Year, firstonset, rep(0,length(firstwar)), my.Blue[4], "white")
legend("topleft", rev(c("New war initiation", "New war ongoing", "Recurring war initiation", "Recurring war ongoing")), fill=rev(c(my.Blue[c(4,2)], my.Red[c(4,1)])), border=rev(c(my.Blue[c(4,2)], my.Red[c(4,1)])), cex=0.75)

detach(cwplot)

#Now control for number of states

load("datasets/states-in-system.RData") 

cwplot.short <- subset(cwplot, Year<2005)

cwplot.rate <- merge(cwplot.short, states.data, by="Year")

attach(cwplot.rate)

# Figure A3

plot(Year, (firstwar+recurrence)/states, type="l", ylim=c(0,0.2), lty=0, ylab="Rate")
pgon(Year, (firstonset+recuronset)/states, firstonset/states, my.Red[4], "white")
pgon(Year, firstonset/states, rep(0,length(firstwar)), my.Blue[4], "white")
legend("topleft", c("New war initiation", "Recurring war initiation"), fill=c(my.Blue[4], my.Red[4]), border=c(my.Blue[4], my.Red[4]), cex=1)

detach(cwplot.rate)

rm(list=ls())

library(ecp)

# Set up a function to draw median lines based on ecp object

draw.median.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], median(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], median(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}
	
draw.mean.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], mean(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], mean(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}

calc.medians <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, median(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

calc.means <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, mean(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

load(file="datasets/Annual-prio-civil.RData") # Annual initiations, derived from UCDP/PRIO Armed Conflict Dataset Version 4-2014a
annual <- annual.prio.civil 

prio.series <- matrix((annual$conflicts/annual$states), ncol=1)
prio.test <- e.divisive(prio.series, min.size=10, R=1000)
prio.test

# Figure A4

plot(annual$Year, prio.series, type="l", col="grey80", ylab=" ", xlab="Year", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Rate of Civil Conflict Initiation, 1946-2014")
draw.median.lines(annual$Year, prio.test$estimates, prio.series)
mtext("Civil conflict initiations per state", side=2, line=3, cex=0.9)
axis(side=1, at=c(1950, 1960, 1970, 1980, 1990, 2000, 2010), labels=c(1950, 1960, 1970, 1980, 1990, 2000, 2010), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

rm(list=ls())
load("datasets/ucdp-prio-acd-181") 
library(doBy)
library(tidyr)
acd$conflict <- 1

foo <- summaryBy(conflict ~ year + type_of_conflict, data=acd, FUN=sum)
data <- as.data.frame(complete(foo, year, type_of_conflict))
data$conflict.sum[is.na(data$conflict.sum)] <- 0

# extrastate, interstate, internationalized civil, civil: 1, 2, 4, 3
colnames(data) <- c("year", "type", "wars")
data1 <- data[data$type==1,]
data2 <- data[data$type==2,]
data3 <- data[data$type==4,]
data4 <- data[data$type==3,]

# Figure A5
par(mar=c(2,4,1,1))
plot(NA, xlim=c(1946, 2017), ylim=c(0,60), xlab=" ", ylab="Number of Conflicts", cex.axis=0.8)
polygon(c(data1$year, rev(data1$year)), c(data1$wars, rep(0, length(data1$wars))), border="black", col="grey60")
polygon(c(data2$year, rev(data2$year), 1946), c((data2$wars+data1$wars), rev(data1$wars), 7), border="black", col="black")
polygon(c(data3$year, rev(data3$year), 1946), c((data3$wars+data2$wars+data1$wars), rev(data2$wars+data1$wars), 9), border="black", col="grey80")
polygon(c(data4$year, rev(data4$year), 1946), c((data4$wars+data3$wars+data2$wars+data1$wars), rev(data3$wars+data2$wars+data1$wars), 17), border="black", col="grey40")

legend("topleft", legend=c("Extrastate", "Interstate", "Internationalized Civil", "Civil"), fill=c("grey60", "black", "grey80", "grey40"), border="white", cex=0.8, bty="n")


rm(list=ls())


library(ecp)

# Set up a function to draw median lines based on ecp object

draw.median.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], median(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], median(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}
	
draw.mean.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], mean(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], mean(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}

calc.medians <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, median(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

calc.means <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, mean(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}

# Figure A6: Four panels, each plotted separately below.

load(file="datasets/Annual.RData")

# Maoz-Russett definition of political relevance, 1816-2011

mid.MR.series <- matrix((annual$force.sum/annual$relevance.MR.sum), ncol=1)
mid.MR.test <- e.divisive(mid.MR.series, min.size=10, R=1000)
mid.MR.test

plot(annual$year, mid.MR.series, type="l", col="grey80", ylab=" ", xlab="Year", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Maoz-Russett")
draw.median.lines(annual$year, mid.MR.test$estimates, mid.MR.series)
mtext("International conflicts per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

# Braumoeller-Carson, with major powers globally relevant, 1816-2011

mid.BC2.series <- matrix((annual$force.sum/annual$relevance.BC2.sum), ncol=1)
mid.BC2.test <- e.divisive(mid.BC2.series, min.size=10, R=1000)
mid.BC2.test

plot(annual$year, mid.BC2.series, type="l", col="grey80", ylab=" ", xlab="Year", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Modified Braumoeller-Carson")
draw.median.lines(annual$year, mid.BC2.test$estimates, mid.BC2.series)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

# Maoz-Russett definition of political relevance, 1816-2011

war.MR.series <- matrix((annual$war.sum/annual$relevance.MR.sum), ncol=1)
war.MR.test <- e.divisive(war.MR.series, min.size=10, R=1000)
war.MR.test

plot(annual$year, war.MR.series, type="l", col="grey80", xlab="Year", ylab=" ", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9)
draw.median.lines(annual$year, war.MR.test$estimates, war.MR.series)
mtext("Interstate wars per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)

# Braumoeller-Carson, with major powers globally relevant, 1816-2011

war.BC2.series <- matrix((annual$war.sum/annual$relevance.BC2.sum), ncol=1)
war.BC2.test <- e.divisive(war.BC2.series, min.size=10, R=1000)
war.BC2.test

plot(annual$year, war.BC2.series, type="l", col="grey80", ylab=" ", xlab="Year", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9)
draw.median.lines(annual$year, war.BC2.test$estimates, war.BC2.series)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)


rm(list=ls())
load("datasets/ucdp-prio-acd-181") 
library(doBy)
library(tidyr)
acd$conflict <- 1

foo <- summaryBy(conflict ~ year + type_of_conflict, data=acd, FUN=sum)
data <- as.data.frame(complete(foo, year, type_of_conflict))
data$conflict.sum[is.na(data$conflict.sum)] <- 0

# extrastate, interstate, internationalized civil, civil: 1, 2, 4, 3
colnames(data) <- c("year", "type", "wars")
data1 <- data[data$type==1,]
data2 <- data[data$type==2,]
data3 <- data[data$type==4,]
data4 <- data[data$type==3,]

# Figure A7
par(mar=c(2,4,1,1))
plot(NA, xlim=c(1946, 2017), ylim=c(0,60), xlab=" ", ylab="Number of Conflicts", cex.axis=0.8)
polygon(c(data1$year, rev(data1$year)), c(data1$wars, rep(0, length(data1$wars))), border="black", col="grey60")
polygon(c(data2$year, rev(data2$year), 1946), c((data2$wars+data1$wars), rev(data1$wars), 7), border="black", col="black")
polygon(c(data3$year, rev(data3$year), 1946), c((data3$wars+data2$wars+data1$wars), rev(data2$wars+data1$wars), 9), border="black", col="grey80")
polygon(c(data4$year, rev(data4$year), 1946), c((data4$wars+data3$wars+data2$wars+data1$wars), rev(data3$wars+data2$wars+data1$wars), 17), border="black", col="grey40")

legend("topleft", legend=c("Extrastate", "Interstate", "Internationalized Civil", "Civil"), fill=c("grey60", "black", "grey80", "grey40"), border="white", cex=0.8, bty="n")


rm(list=ls())

RSH <- read.csv("datasets/IWD10.csv") # Reiter, Stam, Horowitz re-war data

RSH <- RSH[order(RSH$year, RSH$larger_war_id, RSH$init_ccode),]

library(doBy)

RSH.intermediate <- summaryBy(year ~ larger_war_id + init_war_id, data=RSH, FUN=min)
RSH.intermediate$unit <- 1
RSH.series <- summaryBy(unit ~ year.min, data=RSH.intermediate, FUN=sum)
RSH.merge <- merge(RSH.series, annual[,c(1,9)], by.x="year.min", by.y="year", all.y=TRUE)
RSH.merge$unit.sum[is.na(RSH.merge$unit.sum)] <- 0

war.RSH.series <- matrix((RSH.merge$unit.sum/RSH.merge$relevance.BC.sum), ncol=1)
war.RSH.test <- e.divisive(war.RSH.series, min.size=10, R=1000)
war.RSH.test

# Figure A8

plot(RSH.merge$year, war.RSH.series, type="l", col="grey80", ylab=" ", xlab="Year", xaxt="n", yaxt="n", cex.lab=0.9, cex.axis=0.9, main="Reiter/Stam/Horowitz war data")
draw.median.lines(RSH.merge$year, war.RSH.test$estimates, war.RSH.series)
mtext("Interstate wars per relevant dyad", side=2, line=3, cex=0.9)
axis(side=1, at=c(1850, 1900, 1950, 2000), labels=c(1850, 1900, 1950, 2000), cex.axis=0.9)
axis(side=2, cex.axis=0.9)


#################
### Chapter 5 ###
#################


rm(list=ls())

data <- read.csv("datasets/powers.csv", header=FALSE) # Data points from Cederman et al graph
colnames(data) <- c("deaths", "prx")
pre <- data[1:33,]
post <- data[34:54,]

# Figure 5.1
par(mar=c(4,4,1,1))
options(scipen=999)
plot(pre$deaths, pre$prx, log="xy", xlim=c(10000,100000000), ylim=c(0.01,1), ylab="Pr(X>x)", xlab="Battle Deaths", pch=5, bty="L", xaxt="n", yaxt="n", col="#888888")
points(post$deaths, post$prx, pch=3, col="red")
axis(1, at=c(10000, 100000, 1000000, 10000000, 100000000), labels=c("10,000", "100,000", "1,000,000", "10,000,000", "100,000,000"), cex.axis=0.8)
axis(2, at=c(0.01, 0.1, 1), cex.axis=0.8)
model <- lm(log(pre$prx) ~ log(pre$deaths))
lines(pre$deaths, exp(predict(model, newdata=list(x=pre$deaths))) ,col="#888888")
model <- lm(log(post$prx) ~ log(post$deaths))
lines(post$deaths, exp(predict(model, newdata=list(x=post$deaths))) ,col="red")
legend("topright", legend=c("Pre-1789", "Post-1789"), pch=c(5,3), col=c("#888888", "red"), border="white", cex=0.8, bty="n")


# The figures in this chapter are out of order because tests of a given type are easier to run all at once.
# Note that the variable names as constructed in severity-dataset.rda are very misleading:
#    Severity = battle deaths / pooled population (a.k.a. "Intensity")
#    Deaths = raw battle deaths (a.k.a. "Severity")
#    Deaths.world = battle deaths / world population (a.k.a. "Prevalence")

rm(list=ls())

load(file="datasets/severity-dataset.rda")
library(poweRlaw)
library(kSamples)
library(ecp)

draw.median.lines <- function(yearvar, cuts, series){
	for(i in 1:(length(cuts)-1)){
		segments(yearvar[cuts[i]], median(series[cuts[i]:cuts[i+1]-1]),
		yearvar[cuts[i+1]-1], median(series[cuts[i]:cuts[i+1]-1]), col="red")
		}
	}
	
calc.medians <- function(yearvar, cuts, series){
	vec<-NULL
	for(i in 1:(length(cuts)-1)){
		vec <- c(vec,c(cuts[i], cuts[i+1]-1, median(series[cuts[i]:cuts[i+1]-1])))
		}
	mat <- matrix(vec, ncol=3, byrow=TRUE)
	return(mat)
	}


attach(interstate.deaths)
options(scipen=999)

series <- matrix(Severity, ncol=1)

# Figure 5.2

plot(series, type="l", col="grey80", ylab="Battle deaths per 1,000 pooled population", cex.axis=0.9, cex.lab=0.9, xaxt="n", xlab=" ", main="International War Intensity, 1816-2007")
mtext("Wars ordered sequentially", side=1, line=1, cex=0.9)
test <- e.divisive(series, min.size=5, R=1000)
test
abline(h=median(Severity), col="red")


series <- matrix(Deaths, ncol=1)

# Figure 5.6 (top)

plot(series, type="l", col="grey80", ylab="Total battle deaths", xaxt="n", xlab=" ", main="")
mtext("Wars ordered sequentially", side=1, line=1)
test <- e.divisive(series, min.size=5, R=1000)
test
abline(h=median(Deaths), col="red")


series <- matrix(Severity.World, ncol=1)

# Figure 5.8 (top)

plot(series, type="l", col="grey80", ylab="Battle deaths per 1,000 global population", xaxt="n", xlab=" ")
mtext("Wars ordered sequentially", side=1, line=1)
test <- e.divisive(series, min.size=5, R=1000)
test
abline(h=median(series), col="red")

detach(interstate.deaths)

### K-S and power-law tests

p.vec3 <- NULL
p.vec3AD <- NULL

for(i in 3:92){
	p.vec3 <- c(p.vec3, ks.test(interstate.deaths$Severity[1:i], interstate.deaths$Severity[(i+1):95])$p.value)
	p.vec3AD <- c(p.vec3AD, ad.test(interstate.deaths$Severity[1:i], interstate.deaths$Severity[(i+1):95])$ad[,3][1])
	}

# Figure 5.3

plot(c(3:92), p.adjust(p.vec3, method="holm"), ylim=c(1,0), type="l", main="Test for Change in War Intensity", ylab="Pr(test result if no change in distribution)", xlab="International wars, ordered sequentially", xaxt="n")
lines(c(3:92), p.adjust(p.vec3AD, method="holm"), lty=2)  # Closest is 1940; makes sense
abline(h=0.05, lty=1, col="grey80")
legend(3, 0.12, legend=c("Kolmogorov-Smirnov", "Anderson-Darling"), lty=c(1,2), cex=0.8, box.col="white")


m_bl <- conpl$new(interstate.deaths$Severity[1:57])  # New continuous power law object
est1 <- estimate_xmin(m_bl)
est1
m_bl$setXmin(est1)

m_bl2 <- conpl$new(interstate.deaths$Severity[58:95])  # New continuous power law object
est2 <- estimate_xmin(m_bl2)
est2   # xmin = 0.08399955, alpha = 1.625026
m_bl2$setXmin(est2)

powplot <- function(x, x.lab="Data"){
	len <- length(x)
	plot(sort(x), (len:1)/len, log="xy", pch=15, col="#88888844", axes=TRUE, ylab="Pr(X>x)", xlab=x.lab)
	}
	
powpoints <- function(x){
	len <- length(x)
	points(sort(x), (len:1)/len, pch=19, col="#FF000044")
	}
	
# Figure 5.4

powplot(interstate.deaths$Severity[1:57], x.lab="War intensity (battle deaths per 1,000 pooled population)")
lines(m_bl, col="#888888", lwd=2, lty=2)
powpoints(interstate.deaths$Severity[58:95])
lines(m_bl2, col="red", lwd=2)
legend("bottomleft", c("Through World War II", "Post-World War II"), col=c("#888888", "red"), pch=c(15, 19), lty=c(2,1), box.lwd=0, box.col="white")


bs1 <- bootstrap_p(m_bl, no_of_sims=1000, threads=20)
bs1$p

bs2 <- bootstrap_p(m_bl2, no_of_sims=1000, threads=20)
bs2$p

# Figure 5.5 (note that distributions may vary slightly in appearance due to different bootstrap samples)

plot(density(bs1$bootstraps[,3]), yaxt="n", xlim=c(1,3), xlab=expression(paste(alpha)), main="War Intensity", col="white")
polygon(c(density(bs1$bootstraps[,3]), rev(density(bs1$bootstraps[,3])), rep(0, 2*length(bs1$bootstraps[,3]))), border=NA, col="#9ecae188")
polygon(c(density(bs2$bootstraps[,3]), rev(density(bs2$bootstraps[,3])), rep(0, 2*length(bs2$bootstraps[,3]))), border=NA, col="#3182bd88")
legend("topright", c("Through World War II", "Post-World War II"), fill=c("#9ecae188", "#3182bd88"), border="white", box.lwd=0, box.col="white", cex=1)

hist((bs1$bootstraps[,3] - bs2$bootstraps[,3]), breaks="fd", xlab=expression(paste(alpha)))
obs.diff <- est1$pars - est2$pars
sum(abs(bs1$bootstraps[,3] - bs2$bootstraps[,3]) > obs.diff)/length((bs1$bootstraps[,3] - bs2$bootstraps[,3]))



library(Matching)
p.vec3 <- NULL
p.vec3AD <- NULL

for(i in 3:92){
	p.vec3 <- c(p.vec3, ks.boot(interstate.deaths$Deaths[1:i], interstate.deaths$Deaths[(i+1):95], nboots=1000)$ks.boot.pvalue)
	p.vec3AD <- c(p.vec3AD, ad.test(interstate.deaths$Deaths[1:i], interstate.deaths$Deaths[(i+1):95])$ad[,3][1])
	}

# Figure 5.6 (bottom) (note that solid line may vary slightly in appearance due to different bootstrap samples in K-S test)

plot(c(3:92), p.adjust(p.vec3, method="holm"), ylim=c(1,0), type="l", main="Test for Change in War Severity", ylab="Pr(test result if no change in distribution)", xlab="International wars, ordered sequentially", xaxt="n")
lines(c(3:92), p.adjust(p.vec3AD, method="holm"), lty=2)  # Nothinburger
abline(h=0.05, lty=1, col="grey80")
legend(3, 0.12, legend=c("Kolmogorov-Smirnov", "Anderson-Darling"), lty=c(1,2), cex=0.8, box.col="white")


m_bl <- conpl$new(interstate.deaths$Deaths[1:57])  # New continuous power law object
est1 <- estimate_xmin(m_bl)
est1
m_bl$setXmin(est1)

m_bl2 <- conpl$new(interstate.deaths$Deaths[58:95])  # New continuous power law object
est2 <- estimate_xmin(m_bl2)
est2
m_bl2$setXmin(est2)

# Figure 5.7 (top)

powplot(interstate.deaths$Deaths[1:57], x.lab="War severity (raw battle deaths)")
lines(m_bl, col="#888888", lwd=2, lty=2)
powpoints(interstate.deaths$Deaths[58:95])
lines(m_bl2, col="red", lwd=2)
legend("bottomleft", c("Through World War II", "Post-World War II"), col=c("#888888", "red"), pch=c(15, 19), lty=c(2,1), box.lwd=0, box.col="white")


bs1 <- bootstrap_p(m_bl, no_of_sims=1000, threads=20)
bs1$p

bs2 <- bootstrap_p(m_bl2, no_of_sims=1000, threads=20)
bs2$p

# Figure 5.7 (bottom) (note that distributions may vary slightly in appearance due to different bootstrap samples)

plot(density(bs1$bootstraps[,3]), yaxt="n", xlim=c(1,3), xlab=expression(paste(alpha)), main="War Severity", col="white")
polygon(c(density(bs1$bootstraps[,3]), rev(density(bs1$bootstraps[,3])), rep(0, 2*length(bs1$bootstraps[,3]))), border=NA, col="#9ecae188")
polygon(c(density(bs2$bootstraps[,3]), rev(density(bs2$bootstraps[,3])), rep(0, 2*length(bs2$bootstraps[,3]))), border=NA, col="#3182bd88")
legend("topright", c("Through World War II", "Post-World War II"), fill=c("#9ecae188", "#3182bd88"), border="white", box.lwd=0, box.col="white", cex=1)

hist((bs1$bootstraps[,3] - bs2$bootstraps[,3]), breaks="fd", xlab=expression(paste(alpha)))
obs.diff <- est1$pars - est2$pars
sum(abs(bs1$bootstraps[,3] - bs2$bootstraps[,3]) > obs.diff)/length((bs1$bootstraps[,3] - bs2$bootstraps[,3]))


p.vec3 <- NULL
p.vec3AD <- NULL

for(i in 3:92){
	p.vec3 <- c(p.vec3, ks.boot(interstate.deaths$Severity.World[1:i], interstate.deaths$Severity.World[(i+1):95], nboots=10000)$ks.boot.pvalue)
	p.vec3AD <- c(p.vec3AD, ad.test(interstate.deaths$Severity.World[1:i], interstate.deaths$Severity.World[(i+1):95])$ad[,3][1])
	}

# Figure 5.8 (bottom) (note that solid line may vary slightly in appearance due to different bootstrap samples in K-S test)

plot(c(3:92), p.adjust(p.vec3, method="holm"), ylim=c(1,0), type="l", main="Test for Change in War Deaths / 1,000 World Population", ylab="Pr(test result if no change in distribution)", xlab="International wars, ordered sequentially", xaxt="n")
lines(c(3:92), p.adjust(p.vec3AD, method="holm"), lty=2)
abline(h=0.05, lty=1, col="grey80")
legend(3, 0.12, legend=c("Kolmogorov-Smirnov", "Anderson-Darling"), lty=c(1,2), cex=0.8, box.col="white")

# Figure 5.9

plot(ecdf(interstate.deaths$Severity.World[58:95]), verticals=TRUE, pch=NA, col.01line="#FF000000", col="red", ylab="Fraction equally or less deadly", xlab="Battle deaths per 1000 world population", main="Distribution of Fatalities by War")
lines(ecdf(interstate.deaths$Severity.World[1:57]), verticals=TRUE, pch=NA, col.01line="#00000000", col="#88888888")
legend("bottomright", c("Through World War II", "Post-World War II"), lty=c(1,1), col=c("#88888888", "red"), box.lty=0, cex=0.8)


m_bl <- conpl$new(interstate.deaths$Severity.World[1:57])  # New continuous power law object
est1 <- estimate_xmin(m_bl)
est1
m_bl$setXmin(est1)

m_bl2 <- conpl$new(interstate.deaths$Severity.World[58:95])  # New continuous power law object
est2 <- estimate_xmin(m_bl2)
est2
m_bl2$setXmin(est2)

# Figure 5.10 (top)

powplot(interstate.deaths$Severity.World[1:57], x.lab="War deaths per 1,000 world population")
lines(m_bl, col="#888888", lwd=2, lty=2)
powpoints(interstate.deaths$Severity.World[58:95])
lines(m_bl2, col="red", lwd=2)
legend("bottomleft", c("Through World War II", "Post-World War II"), col=c("#888888", "red"), pch=c(15, 19), lty=c(2,1), box.lwd=0, box.col="white")


bs1 <- bootstrap_p(m_bl, no_of_sims=1000, threads=20)
bs1$p

bs2 <- bootstrap_p(m_bl2, no_of_sims=1000, threads=20)
bs2$p

# Figure 5.10 (bottom) (note that distributions may vary slightly in appearance due to different bootstrap samples)

plot(density(bs1$bootstraps[,3]), yaxt="n", xlim=c(1,3), xlab=expression(paste(alpha)), main="War Deaths per 1,000 World Population", col="white")
polygon(c(density(bs1$bootstraps[,3]), rev(density(bs1$bootstraps[,3])), rep(0, 2*length(bs1$bootstraps[,3]))), border=NA, col="#9ecae188")
polygon(c(density(bs2$bootstraps[,3]), rev(density(bs2$bootstraps[,3])), rep(0, 2*length(bs2$bootstraps[,3]))), border=NA, col="#3182bd88")
legend("topright", c("Through World War II", "Post-World War II"), fill=c("#9ecae188", "#3182bd88"), border="white", box.lwd=0, box.col="white", cex=1)

# Figure 5.12

plot(log(interstate.deaths$Population), log(interstate.deaths$Deaths/interstate.deaths$Population), type="n", xaxt="n", yaxt="n", ylab="Battle deaths per 1,000 combatant population", xlab="Pooled population of combatants, in millions") #Battle deaths per 1,000 population, and population in 1000s (divide by 1000 in labels to get millions)
interstate.deaths$col <- "#7570b3"
interstate.deaths$col[interstate.deaths$Year>1900] <- "#1b9e77"
interstate.deaths$col[interstate.deaths$Year>1945] <- "#d95f02"
axis(1, at=c(log(1000), log(10000), log(100000), log(1000000)), labels=c("1", "10", "100", "1000"))
axis(2, at=c(log(0.001), log(0.01), log(0.1), log(1), log(10)), labels=c("0.001", "0.01", "0.1", "1", "10"))
text(log(interstate.deaths$Population), log(interstate.deaths$Deaths/interstate.deaths$Population), label=interstate.deaths$Year, col=interstate.deaths$col, cex=0.6)
for(i in c("#7570b3", "#1b9e77", "#d95f02")){
     abline(lm(log(interstate.deaths$Deaths[interstate.deaths$col==i]/interstate.deaths$Population[interstate.deaths$col==i]) ~ log(interstate.deaths$Population[interstate.deaths$col==i])), col=i)
}
legend("bottomleft", legend=c("19th century", "1901-1945", "Post-1945"), fill=c("#7570b3", "#1b9e77", "#d95f02"), border="white", box.lwd=0, box.col=NA, cex=0.8)

library(stargazer) 
# note that variable names start with l rather than 1 to avoid making R choke
# Leave out 19th century as baseline category
interstate.deaths$l9011945 <- 0
interstate.deaths$l9011945[(interstate.deaths$Year>1900) & (interstate.deaths$Year<1946)] <- 1
interstate.deaths$Post45 <- 0
interstate.deaths$Post45[interstate.deaths$Year>1945] <- 1

model.1 <- lm(log(interstate.deaths$Deaths/interstate.deaths$Population) ~ log(interstate.deaths$Population) + interstate.deaths$Year)
model.2 <- lm(log(interstate.deaths$Deaths/interstate.deaths$Population) ~ log(interstate.deaths$Population) + interstate.deaths$l9011945 + interstate.deaths$Post45)

# Table A.1

stargazer(model.1, model.2, type="text", title="Results", align=TRUE, star.cutoffs=c(0.05, 0.01, 0.001))

options(scipen=999)
library(scales)


powlineplot <- function(x){
	len <- length(x)
	lines(sort(x), (len:1)/len, col="#0000000A", type="l")
	}

powpoints <- function(x){
	len <- length(x)
	points(sort(x), (len:1)/len, pch=19, col="#FF000044")
	}

pval <- numeric(0)


duration250 <- numeric(0)

for(iter in 1:250){
# set.seed(0)
duration <- numeric(0)

maxdur <- rep(10000, 95)
# maxdur[1:167] <- round(abs(rnorm(167, mean=0, sd=20)))

for(j in 1:95){
	g1 <- 10
	g2 <- 10
	i <- 1
	while(g1>0 & g2>0 & i<maxdur[j]){
		foo <- runif(1)>0.5
		if(foo){
			g1 <- g1-1
			g2 <- g2+1
			} else{
			g1 <- g1+1
			g2 <- g2-1
			}
		i <- i + 1
		}
	duration <- c(duration, (i))
	}
duration250 <- c(duration250, duration)
}

maxvec <- numeric(0)

for(k in 1:250){
	maxvec <- c(maxvec, max(duration250[(95*(k-1)+1):(95*k)]))
}

scaled.log.duration <- rescale(log(duration250), to=c(min(log(interstate.deaths$Severity)), max(log(interstate.deaths$Severity))), from=c(log(11), log(mean(maxvec))+0.1))

scaled.duration <- exp(scaled.log.duration)

# Figure 5.13; again, simulation variation will likely produce slight variations in results.

plot(1, type="n", log="xy", pch=19, col="#88888844", axes=TRUE, ylab="Pr(X>x)", xlab="War intensity (battle deaths per 1,000 pooled population)", ylim=c(0.01,1), xlim=exp(c(-7,5)))
for(sd in 1:250){
	powlineplot(sort(scaled.duration[(95*(sd-1)+1):(95*sd)]))
}
powpoints(sort(interstate.deaths$Severity))


#################
### Chapter 7 ###
#################

rm(list=ls())

data <- read.csv("datasets/Holsti.csv")
source("datasets/slopegraph.r")
library(plyr)
library(ggplot2)

library(reshape2)
data.long <- melt(data, id="X")[1:96,]
colnames(data.long) <- c("Issue", "Years", "Percentage")
data.long$Years <- as.character(data.long$Years)
data.long$Years <- gsub("X", "", data.long$Years)
data.long$Years <- gsub("\\.", "-", data.long$Years)

##' Convert raw data to right format
df <- build_slopegraph(data.long, x="Years", y="Percentage", group="Issue", method="tufte", min.space=0.13)

## Refactor the x-axis to get the right labels, round the y values for presentation
# df <- transform(df, x=factor(x, levels=c(5,10,15,20), labels=c("5 years", "10 years", "15 years", "20 years")), y=round(y))
##' Generate the raw plot
gg.form <- plot_slopegraph(df) + labs(title="Percentage of wars fought over...") 

# Figure 7.1 
plot(gg.form)


#################
### Chapter 8 ###
#################

rm(list=ls())

load(file="datasets/BCSTS.RData") # Only reciprocated MIDs, of level 4 and above

myvars <- c("year", "ccode1", "ccode2", "force", "war", "cap_1", "cap_2", "majpow1", "majpow2", "contig", "distance")
mydata <- BCSTS.big[myvars]

rm(list=(ls()[ls()!="mydata"])) 

# Not all the variables needed to calculate relevance are available to the end of the
# time series, but things like distance just don't change much, so last-value
# imputation is pretty safe here.
# Last-value imputation for contiguity, distance, major power status
contig.imp <- mydata[c("year", "ccode1", "ccode2", "contig")][mydata$year==2006,]
dist.imp <- mydata[c("year", "ccode1", "ccode2", "distance")][mydata$year==2008,]
mp1.imp <- mydata[c("year", "ccode1", "ccode2", "majpow1")][mydata$year==2008,]
mp2.imp <- mydata[c("year", "ccode1", "ccode2", "majpow2")][mydata$year==2008,]
cap1.imp <- mydata[c("year", "ccode1", "ccode2", "cap_1")][mydata$year==2007,]
cap2.imp <- mydata[c("year", "ccode1", "ccode2", "cap_2")][mydata$year==2007,]

for(year.counter in 2007:2011){
	contig.imp$year <- year.counter
	mydata <- merge(mydata, contig.imp, by=c("ccode1", "ccode2", "year"), all.x=TRUE)
	mydata$contig.x[mydata$year==year.counter] <- mydata$contig.y[mydata$year==year.counter]
	names(mydata)[names(mydata) == "contig.x"] <- "contig"
	mydata <- subset(mydata, select=-contig.y)
}

for(year.counter in 2009:2011){
	dist.imp$year <- year.counter
	mydata <- merge(mydata, dist.imp, by=c("ccode1", "ccode2", "year"), all.x=TRUE)
	mydata$distance.x[mydata$year==year.counter] <- mydata$distance.y[mydata$year==year.counter]
	names(mydata)[names(mydata) == "distance.x"] <- "distance"
	mydata <- subset(mydata, select=-distance.y)
}

for(year.counter in 2009:2011){
	mp1.imp$year <- year.counter
	mydata <- merge(mydata, mp1.imp, by=c("ccode1", "ccode2", "year"), all.x=TRUE)
	mydata$majpow1.x[mydata$year==year.counter] <- mydata$majpow1.y[mydata$year==year.counter]
	names(mydata)[names(mydata) == "majpow1.x"] <- "majpow1"
	mydata <- subset(mydata, select=-majpow1.y)
}

for(year.counter in 2009:2011){
	mp2.imp$year <- year.counter
	mydata <- merge(mydata, mp2.imp, by=c("ccode1", "ccode2", "year"), all.x=TRUE)
	mydata$majpow2.x[mydata$year==year.counter] <- mydata$majpow2.y[mydata$year==year.counter]
	names(mydata)[names(mydata) == "majpow2.x"] <- "majpow2"
	mydata <- subset(mydata, select=-majpow2.y)
}

for(year.counter in 2008:2011){
	cap1.imp$year <- year.counter
	mydata <- merge(mydata, cap1.imp, by=c("ccode1", "ccode2", "year"), all.x=TRUE)
	mydata$cap_1.x[mydata$year==year.counter] <- mydata$cap_1.y[mydata$year==year.counter]
	names(mydata)[names(mydata) == "cap_1.x"] <- "cap_1"
	mydata <- subset(mydata, select=-cap_1.y)
}

for(year.counter in 2008:2011){
	cap2.imp$year <- year.counter
	mydata <- merge(mydata, cap2.imp, by=c("ccode1", "ccode2", "year"), all.x=TRUE)
	mydata$cap_2.x[mydata$year==year.counter] <- mydata$cap_2.y[mydata$year==year.counter]
	names(mydata)[names(mydata) == "cap_2.x"] <- "cap_2"
	mydata <- subset(mydata, select=-cap_2.y)
}


mydata$majdyad <- pmax(mydata$majpow1, mydata$majpow2)
mydata$cap_max <- pmax(mydata$cap_1, mydata$cap_2)  # temp

logit<-function(a){
	1/(1+exp(-1*a))
}

mydata$contig2 <- mydata$contig

# Contiguity needs to be recoded.
# Maoz and Russett used land border or <150mi water border.
# Those are coded 1-4 in the present data.

mydata$contig[mydata$contig<5] <- 1
mydata$contig[mydata$contig>4] <- 0

# Worth checking out what a 400-mile limit implies.

mydata$contig2[mydata$contig2<6] <- 1
mydata$contig2[mydata$contig2>5] <- 0

# Calculate the Maoz-Russett metric of relevance
mydata$relevance.MR <- pmax(mydata$majdyad, mydata$contig)
mydata$relevance.MR2 <- pmax(mydata$majdyad, mydata$contig2)

# Calculate the Braumoeller/Carson metric of relevance
mydata$relevance.BC <- logit(4.801 + 4.50*mydata$contig - 1.051*log(mydata$distance) + 2.901*mydata$majdyad)

# Add codings from Andy Goodhart for 19th and 20th century international orders
# Very generous definition of international order to avoid missing any
# Also codes new states that arose from the disintegration of order

# Format of variable names is c1.xxx and c2.xxx for c1, c2. xxx's are:
# LatinAm: Latin American Decolonization from 1810-1858
# concert: Concert of Europe from 1816-1852
# concert.gp: Concert of Europe, Great Powers only
# interim: period btw Concert and Bismarck
# bismarck: Bismarckian order
# IndependentAfterWWI: Newly independent states after WWI
# MandateAfterWWI: Middle East territories under int'l mandate (coded by countries that emerged)
# League: League of Nations
# PostWarLiberal: Post-WWII liberal international order
# PostWarCommunist: Post-WWII communist international order
# PostWarOther: Third World and nonaligned middle powers

# Latin American Decolonization from 1810-1858. Note that 1858 is 30 years after the last declaration of independence in this period (i.e., Uruguay in 1828).

mydata$c1.LatinAm<-0
mydata$c1.LatinAm[mydata$ccode1==160 & mydata$year>1815 & mydata$year<1857]<- 1 # Argentina declared independence in 1816.
mydata$c1.LatinAm[mydata$ccode1==145 & mydata$year>1824 & mydata$year<1857]<- 1 # Bolivia gained indepdendence in 1825.
mydata$c1.LatinAm[mydata$ccode1==140 & mydata$year>1821 & mydata$year<1857]<- 1 # Brazil gained independence in 1822.
mydata$c1.LatinAm[mydata$ccode1==155 & mydata$year>1809 & mydata$year<1857]<- 1 # Chile broke from Spanish rule in 1810 and the royalist forces collapsed in 1826.
mydata$c1.LatinAm[mydata$ccode1==100 & mydata$year>1819 & mydata$year<1857]<- 1 # Colombia gained independence from the Spanish in 1820.
mydata$c1.LatinAm[mydata$ccode1==94 & mydata$year>1820 & mydata$year<1857]<- 1 # Costa Rica gained independence in 1821.
mydata$c1.LatinAm[mydata$ccode1==130 & mydata$year>1821 & mydata$year<1857]<- 1 # Ecuador gained independence in 1822.
mydata$c1.LatinAm[mydata$ccode1==92 & mydata$year>1820 & mydata$year<1857]<- 1 # El Salvador gained independence in 1821.
mydata$c1.LatinAm[mydata$ccode1==90 & mydata$year>1820 & mydata$year<1857]<- 1 # Guatemala gained independence in 1821.
mydata$c1.LatinAm[mydata$ccode1==91 & mydata$year>1820 & mydata$year<1857]<- 1 # Honduras gained independence in 1821.
mydata$c1.LatinAm[mydata$ccode1==70 & mydata$year>1820 & mydata$year<1857]<- 1 # Mexico gained independence in 1821.
mydata$c1.LatinAm[mydata$ccode1==93 & mydata$year>1820 & mydata$year<1857]<- 1 # Nicaragua gained independence in 1821 but control of the country was not yet consolidated.
mydata$c1.LatinAm[mydata$ccode1==95 & mydata$year>1820 & mydata$year<1857]<- 1 # Panama gained independence in 1821.
mydata$c1.LatinAm[mydata$ccode1==135 & mydata$year>1820 & mydata$year<1857]<- 1 # Peru declared independence in 1821 and defeated the Spanish in 1824.
mydata$c1.LatinAm[mydata$ccode1==150 & mydata$year>1810 & mydata$year<1857]<- 1 # Paraguay gained independence in 1811.
mydata$c1.LatinAm[mydata$ccode1==165 & mydata$year>1827 & mydata$year<1857]<- 1 # Uruguay gained independence in 1828.
mydata$c1.LatinAm[mydata$ccode1==101 & mydata$year>1820 & mydata$year<1857]<- 1 # Venezuela gained independence in 1821.

mydata$c2.LatinAm<-0
mydata$c2.LatinAm[mydata$ccode2==160 & mydata$year>1815 & mydata$year<1857]<- 1 # Argentina declared independence in 1816.
mydata$c2.LatinAm[mydata$ccode2==145 & mydata$year>1824 & mydata$year<1857]<- 1 # Bolivia gained indepdendence in 1825.
mydata$c2.LatinAm[mydata$ccode2==140 & mydata$year>1821 & mydata$year<1857]<- 1 # Brazil gained independence in 1822.
mydata$c2.LatinAm[mydata$ccode2==155 & mydata$year>1809 & mydata$year<1857]<- 1 # Chile broke from Spanish rule in 1810 and the royalist forces collapsed in 1826.
mydata$c2.LatinAm[mydata$ccode2==100 & mydata$year>1819 & mydata$year<1857]<- 1 # Colombia gained independence from the Spanish in 1820.
mydata$c2.LatinAm[mydata$ccode2==94 & mydata$year>1820 & mydata$year<1857]<- 1 # Costa Rica gained independence in 1821.
mydata$c2.LatinAm[mydata$ccode2==130 & mydata$year>1821 & mydata$year<1857]<- 1 # Ecuador gained independence in 1822.
mydata$c2.LatinAm[mydata$ccode2==92 & mydata$year>1820 & mydata$year<1857]<- 1 # El Salvador gained independence in 1821.
mydata$c2.LatinAm[mydata$ccode2==90 & mydata$year>1820 & mydata$year<1857]<- 1 # Guatemala gained independence in 1821.
mydata$c2.LatinAm[mydata$ccode2==91 & mydata$year>1820 & mydata$year<1857]<- 1 # Honduras gained independence in 1821.
mydata$c2.LatinAm[mydata$ccode2==70 & mydata$year>1820 & mydata$year<1857]<- 1 # Mexico gained independence in 1821.
mydata$c2.LatinAm[mydata$ccode2==93 & mydata$year>1820 & mydata$year<1857]<- 1 # Nicaragua gained independence in 1821 but control of the country was not yet consolidated.
mydata$c2.LatinAm[mydata$ccode2==95 & mydata$year>1820 & mydata$year<1857]<- 1 # Panama gained independence in 1821.
mydata$c2.LatinAm[mydata$ccode2==135 & mydata$year>1820 & mydata$year<1857]<- 1 # Peru declared independence in 1821 and defeated the Spanish in 1824.
mydata$c2.LatinAm[mydata$ccode2==150 & mydata$year>1810 & mydata$year<1857]<- 1 # Paraguay gained independence in 1811.
mydata$c2.LatinAm[mydata$ccode2==165 & mydata$year>1827 & mydata$year<1857]<- 1 # Uruguay gained independence in 1828.
mydata$c2.LatinAm[mydata$ccode2==101 & mydata$year>1820 & mydata$year<1857]<- 1 # Venezuela gained independence in 1821.


# Concert of Europe from 1816-1852: Includes all states in Europe (CCODES 199-399)

mydata$c1.concert<-0
mydata$c1.concert[mydata$ccode1>199 & mydata$ccode1<400 & mydata$year>1815 & mydata$year<1853]<- 1

mydata$c2.concert<- 0
mydata$c2.concert[mydata$ccode2>199 & mydata$ccode2<400 & mydata$year>1815 & mydata$year<1853]<- 1

mydata$c1.concert.gp<-0
mydata$c1.concert.gp[mydata$ccode1 %in% c(200,220,255,300,365) & mydata$year>1815 & mydata$year<1853]<- 1

mydata$c2.concert.gp<- 0
mydata$c2.concert.gp[mydata$ccode2 %in% c(200,220,255,300,365) & mydata$year>1815 & mydata$year<1853]<- 1


# Bismarckian Order from 1855-1870: Includes all states in Europe (CCODES 199-399)

mydata$c1.interim<-0
mydata$c1.interim[mydata$ccode1>199 & mydata$ccode1<400 & mydata$year>1854 & mydata$year<1871]<- 1

mydata$c2.interim<- 0
mydata$c2.interim[mydata$ccode2>199 & mydata$ccode2<400 & mydata$year>1854 & mydata$year<1871]<- 1

# Bismarckian Order from 1871-1890: Includes all states in Europe (CCODES 199-399)

mydata$c1.bismarck<-0
mydata$c1.bismarck[mydata$ccode1>199 & mydata$ccode1<400 & mydata$year>1870 & mydata$year<1891]<- 1

mydata$c2.bismarck<- 0
mydata$c2.bismarck[mydata$ccode2>199 & mydata$ccode2<400 & mydata$year>1870 & mydata$year<1891]<- 1



# Wilhelmine System from 1890-1913

mydata$c1.wilhelm<-0
mydata$c1.wilhelm[mydata$ccode1>199 & mydata$ccode1<400 & mydata$year>1889 & mydata$year<1915]<- 1

mydata$c2.wilhelm<- 0
mydata$c2.wilhelm[mydata$ccode2>199 & mydata$ccode2<400 & mydata$year>1889 & mydata$year<1915]<- 1

# Newly Independent States after the First World War (Source: https://en.wikipedia.org/wiki/Aftermath_of_World_War_I#New_nations_break_free; https://www.britannica.com/place/Yugoslavia-former-federated-nation-1929-2003;
# https://www.loc.gov/law/help/us-treaties/bevans/m-ust000002-0043.pdf; and various pages in the CIA World Factbook and Encyclopedia Britannica online; https://history.state.gov/countries/egypt

mydata$c1.IndependentAfterWWI<-0
mydata$c1.IndependentAfterWWI[mydata$ccode1==700 & mydata$year>1918 & mydata$year<1939]<-1 # Afghanistan gained independence from Britain in 1919.
mydata$c1.IndependentAfterWWI[mydata$ccode1==339 & mydata$year>1911 & mydata$year<1939]<-1 # Albania gained independence from the Ottomans in 1912.
mydata$c1.IndependentAfterWWI[mydata$ccode1==371 & mydata$year>1917 & mydata$year<1921]<-1 # Armenia was briefly independent from 1918 to 1920.
mydata$c1.IndependentAfterWWI[mydata$ccode1==305 & mydata$year>1917 & mydata$year<1939]<-1 # Austria emerged as an independent country in 1918 and remained so until the German Anschluss of 1938.
mydata$c1.IndependentAfterWWI[mydata$ccode1==373 & mydata$year>1917 & mydata$year<1921]<-1 # Azerbaijan was briefly independent from 1918-1920 but had imperfect control over its territory. The Russians invaded in 1920.
mydata$c1.IndependentAfterWWI[mydata$ccode1==355 & mydata$year>1907 & mydata$year<1939]<-1 # Bulgaria gained independence from the Ottomans in 1908.
mydata$c1.IndependentAfterWWI[mydata$ccode1==315 & mydata$year>1917 & mydata$year<1940]<-1 # Czechoslovakia was established in 1918. Germany annexed part of it in 1938.
mydata$c1.IndependentAfterWWI[mydata$ccode1==651 & mydata$year>1921 & mydata$year<1940]<-1 # Egypt gained indendepence in 1922.
mydata$c1.IndependentAfterWWI[mydata$ccode1==366 & mydata$year>1917 & mydata$year<1940]<-1 # Estonia declared indendepence in 1918. 
mydata$c1.IndependentAfterWWI[mydata$ccode1==375 & mydata$year>1916 & mydata$year<1940]<-1 # Finland gained independence in 1917.
mydata$c1.IndependentAfterWWI[mydata$ccode1==372 & mydata$year>1917 & mydata$year<1922]<-1 # Georgia was briefly independent from 1918-1921. 
mydata$c1.IndependentAfterWWI[mydata$ccode1==310 & mydata$year>1917 & mydata$year<1940]<-1 # Hungary emerged as an independent country in 1918.
mydata$c1.IndependentAfterWWI[mydata$ccode1==205 & mydata$year>1920 & mydata$year<1940]<-1 # Ireland gained independence from Britain in 1921. 
mydata$c1.IndependentAfterWWI[mydata$ccode1==367 & mydata$year>1917 & mydata$year<1940]<-1 # Latvia gained independence in 1918. 
mydata$c1.IndependentAfterWWI[mydata$ccode1==368 & mydata$year>1917 & mydata$year<1940]<-1 # Lithuania gained independence in 1918.
mydata$c1.IndependentAfterWWI[mydata$ccode1==290 & mydata$year>1917 & mydata$year<1940]<-1 # Poland gained independence in 1918.
mydata$c1.IndependentAfterWWI[mydata$ccode1==670 & mydata$year>1931 & mydata$year<1939]<-1 # Saudi Arabia became independent and unified in 1932.
mydata$c1.IndependentAfterWWI[mydata$ccode1==369 & mydata$year>1917 & mydata$year<1921]<-1 # Ukraine was briefly independent from 1918-1920. 
mydata$c1.IndependentAfterWWI[mydata$ccode1==678 & mydata$year>1917 & mydata$year<1939]<-1 # Yemen (Yemen Arab Republic/North Yemen) gained independence from the Ottomans in 1918.
mydata$c1.IndependentAfterWWI[mydata$ccode1==345 & mydata$year>1918 & mydata$year<1940]<-1 # Yugoslavia (known as Kingdom of Serbs, Croats, and Slovenes est. by Paris Peace Conf. in 1919 and became Yugoslavia in 1929)

mydata$c2.IndependentAfterWWI<-0
mydata$c2.IndependentAfterWWI[mydata$ccode2==700 & mydata$year>1918 & mydata$year<1939]<-1 # Afghanistan gained independence from Britain in 1919.
mydata$c2.IndependentAfterWWI[mydata$ccode2==339 & mydata$year>1911 & mydata$year<1939]<-1 # Albania gained independence from the Ottomans in 1912.
mydata$c2.IndependentAfterWWI[mydata$ccode2==371 & mydata$year>1917 & mydata$year<1921]<-1 # Armenia was briefly independent from 1918 to 1920.
mydata$c2.IndependentAfterWWI[mydata$ccode2==305 & mydata$year>1917 & mydata$year<1939]<-1 # Austria emerged as an independent country in 1918 and remained so until the German Anschluss of 1938.
mydata$c2.IndependentAfterWWI[mydata$ccode2==373 & mydata$year>1917 & mydata$year<1921]<-1 # Azerbaijan was briefly independent from 1918-1920 but had imperfect control over its territory. The Russians invaded in 1920.
mydata$c2.IndependentAfterWWI[mydata$ccode2==355 & mydata$year>1907 & mydata$year<1939]<-1 # Bulgaria gained independence from the Ottomans in 1908.
mydata$c2.IndependentAfterWWI[mydata$ccode2==315 & mydata$year>1917 & mydata$year<1940]<-1 # Czechoslovakia was established in 1918. Germany annexed part of it in 1938.
mydata$c1.IndependentAfterWWI[mydata$ccode2==651 & mydata$year>1921 & mydata$year<1940]<-1 # Egypt gained indendepence in 1922.
mydata$c2.IndependentAfterWWI[mydata$ccode2==366 & mydata$year>1917 & mydata$year<1940]<-1 # Estonia declared indendepence in 1918. 
mydata$c2.IndependentAfterWWI[mydata$ccode2==375 & mydata$year>1916 & mydata$year<1940]<-1 # Finland gained independence in 1917.
mydata$c2.IndependentAfterWWI[mydata$ccode2==372 & mydata$year>1917 & mydata$year<1922]<-1 # Georgia was briefly independent from 1918-1921. 
mydata$c2.IndependentAfterWWI[mydata$ccode2==310 & mydata$year>1917 & mydata$year<1940]<-1 # Hungary emerged as an independent country in 1918.
mydata$c2.IndependentAfterWWI[mydata$ccode2==205 & mydata$year>1920 & mydata$year<1940]<-1 # Ireland gained independence from Britain in 1921. 
mydata$c2.IndependentAfterWWI[mydata$ccode2==367 & mydata$year>1917 & mydata$year<1940]<-1 # Latvia gained independence in 1918. 
mydata$c2.IndependentAfterWWI[mydata$ccode2==368 & mydata$year>1917 & mydata$year<1940]<-1 # Lithuania gained independence in 1918.
mydata$c2.IndependentAfterWWI[mydata$ccode2==290 & mydata$year>1917 & mydata$year<1940]<-1 # Poland gained independence in 1918.
mydata$c2.IndependentAfterWWI[mydata$ccode2==670 & mydata$year>1931 & mydata$year<1939]<-1 # Saudi Arabia became independent and unified in 1932.
mydata$c2.IndependentAfterWWI[mydata$ccode2==369 & mydata$year>1917 & mydata$year<1921]<-1 # Ukraine was briefly independent from 1918-1920. 
mydata$c2.IndependentAfterWWI[mydata$ccode2==678 & mydata$year>1917 & mydata$year<1939]<-1 # Yemen (Yemen Arab Republic/North Yemen) gained independence from the Ottomans in 1918.
mydata$c2.IndependentAfterWWI[mydata$ccode2==345 & mydata$year>1918 & mydata$year<1940]<-1 # Yugoslavia (known as Kingdom of Serbs, Croats, and Slovenes est. by Paris Peace Conf. in 1919 and became Yugoslavia in 1929)

# Middle East territories under International Mandate, post-World War I (coded by the countries that emerged from those territories).  
# Note that this section does not include Middle East territories under British control/influence (but not formal mandate) during this period. 
# (UAE, Bahrain, Qatar, Kuwait had relationships with Britain that predate WWI.)
# Present day Palestine (West Bank & Gaza) does not have a CCODE so it is absent here. 
# Sourcs: https://www.britannica.com/place/Syria/The-French-mandate; https://history.state.gov/countries/syria; https://history.state.gov/countries/lebanon; 
# https://en.wikipedia.org/wiki/Anglo-Iraqi_Treaty; https://en.wikipedia.org/wiki/British_Mandate_for_Mesopotamia_(legal_instrument);

mydata$c1.MandateAfterWWI<-0
mydata$c1.MandateAfterWWI[mydata$ccode1==652 & mydata$year>1921 & mydata$year<1940]<-1 # Syria: French Mandate started in 1922
mydata$c1.MandateAfterWWI[mydata$ccode1==660 & mydata$year>1921 & mydata$year<1940]<-1 # Lebanon: French Mandate started in 1922
mydata$c1.MandateAfterWWI[mydata$ccode1==645 & mydata$year>1921 & mydata$year<1940]<-1 # Iraq: British Mandate for Mesopotamia enacted 1922
mydata$c1.MandateAfterWWI[mydata$ccode1==666 & mydata$year>1921 & mydata$year<1940]<-1 # Israel: British Mandate for Palestine enacted 1922
mydata$c1.MandateAfterWWI[mydata$ccode1==663 & mydata$year>1921 & mydata$year<1940]<-1 # Jordan: British Mandate for Palestine enacted 1922

mydata$c2.MandateAfterWWI<-0
mydata$c2.MandateAfterWWI[mydata$ccode2==652 & mydata$year>1921 & mydata$year<1940]<-1 # Syria: French Mandate started in 1922
mydata$c2.MandateAfterWWI[mydata$ccode2==660 & mydata$year>1921 & mydata$year<1940]<-1 # Lebanon: French Mandate started in 1922
mydata$c2.MandateAfterWWI[mydata$ccode2==645 & mydata$year>1921 & mydata$year<1940]<-1 # Iraq: British Mandate for Mesopotamia enacted 1922
mydata$c2.MandateAfterWWI[mydata$ccode2==666 & mydata$year>1921 & mydata$year<1940]<-1 # Israel: British Mandate for Palestine enacted 1922
mydata$c2.MandateAfterWWI[mydata$ccode2==663 & mydata$year>1921 & mydata$year<1940]<-1 # Jordan: British Mandate for Palestine enacted 1922

# Leage of Nations System from 1919-1939 (Source: http://www.indiana.edu/~league/nationalmember.htm, accessed 9 Feb 2017)

mydata$c1.League<-0
mydata$c1.League[mydata$ccode1==700 & mydata$year>1933 & mydata$year<1940]<- 1 # Afghanistan 1934-1939
mydata$c1.League[mydata$ccode1==339 & mydata$year>1919 & mydata$year<1940]<- 1 # Albania 1920-1939(annexed by Italy April, 12 1939)
mydata$c1.League[mydata$ccode1==160 & mydata$year>1919 & mydata$year<1940]<- 1 # Argentina 1920-1939
mydata$c1.League[mydata$ccode1==900 & mydata$year>1919 & mydata$year<1940]<- 1 # Australia 1920-1939
mydata$c1.League[mydata$ccode1==305 & mydata$year>1919 & mydata$year<1939]<- 1 # Austria 1920-1939 (annexed by Germany April 10, 1938)
mydata$c1.League[mydata$ccode1==211 & mydata$year>1919 & mydata$year<1940]<- 1 # Belgium 1920-1939
mydata$c1.League[mydata$ccode1==145 & mydata$year>1919 & mydata$year<1940]<- 1 # Bolivia 1920-1939
mydata$c1.League[mydata$ccode1==140 & mydata$year>1919 & mydata$year<1927]<- 1 # Brazil 1920-1926
mydata$c1.League[mydata$ccode1==200 & mydata$year>1919 & mydata$year<1940]<- 1 # United Kingdom 1920-1939 # NOTE THAT BRITISH EMPIRE WAS THE SAME -- NEED TO ADD THEM. 
mydata$c1.League[mydata$ccode1==355 & mydata$year>1919 & mydata$year<1940]<- 1 # Bulgaria 1920-1939
mydata$c1.League[mydata$ccode1==20 & mydata$year>1919 & mydata$year<1940]<- 1 # Canada 1920-1939
mydata$c1.League[mydata$ccode1==155 & mydata$year>1919 & mydata$year<1939]<- 1 # Chile 1920-1938
mydata$c1.League[mydata$ccode1==710 & mydata$year>1919 & mydata$year<1940]<- 1 # China 1920-1939
mydata$c1.League[mydata$ccode1==100 & mydata$year>1919 & mydata$year<1940]<- 1 # Colombia 1920-1939
mydata$c1.League[mydata$ccode1==94 & mydata$year>1919 & mydata$year<1926]<- 1 # Costa Rica 1920-1925
mydata$c1.League[mydata$ccode1==40 & mydata$year>1919 & mydata$year<1940]<- 1 # Cuba 1920-1939
mydata$c1.League[mydata$ccode1==315 & mydata$year>1919 & mydata$year<1940]<- 1 # Czechoslovakia 1920-1939 (annexed by Germany March 15, 1939)
mydata$c1.League[mydata$ccode1==390 & mydata$year>1919 & mydata$year<1940]<- 1 # Denmark 1920-1939
mydata$c1.League[mydata$ccode1==42 & mydata$year>1923 & mydata$year<1940]<- 1 # Dominican Republic 1924-1939
mydata$c1.League[mydata$ccode1==130 & mydata$year>1933 & mydata$year<1940]<- 1 # Ecuador 1934-1939
mydata$c1.League[mydata$ccode1==651 & mydata$year>1936 & mydata$year<1940]<- 1 # Egypt 1937-1939
mydata$c1.League[mydata$ccode1==366 & mydata$year>1920 & mydata$year<1940]<- 1 # Estonia 1921-1939
mydata$c1.League[mydata$ccode1==530 & mydata$year>1922 & mydata$year<1937]<- 1 # Ethiopia 1923-1936 (annexed by Italy May 9, 1936)
mydata$c1.League[mydata$ccode1==375 & mydata$year>1919 & mydata$year<1940]<- 1 # Finland 1920-1939
mydata$c1.League[mydata$ccode1==220 & mydata$year>1919 & mydata$year<1942]<- 1 # France 1920-1941 ### WHY WAS THIS LATER? DID THEY JUST NOT FORMALLY WITHDRAW UNTIL THEN?
mydata$c1.League[mydata$ccode1==255 & mydata$year>1925 & mydata$year<1934]<- 1 # Germany 1926-1933
mydata$c1.League[mydata$ccode1==350 & mydata$year>1919 & mydata$year<1940]<- 1 # Greece 1920-1939
mydata$c1.League[mydata$ccode1==90 & mydata$year>1919 & mydata$year<1937]<- 1 # Guatemala 1920-1936
mydata$c1.League[mydata$ccode1==41 & mydata$year>1919 & mydata$year<1940]<- 1 # Haiti 1920-1942 ### CHECK THIS END DATE
mydata$c1.League[mydata$ccode1==91 & mydata$year>1919 & mydata$year<1937]<- 1 # Honduras 1920-1936
mydata$c1.League[mydata$ccode1==310 & mydata$year>1921 & mydata$year<1940]<- 1 # Hungary 1922-1939
mydata$c1.League[mydata$ccode1==750 & mydata$year>1919 & mydata$year<1940]<- 1 # India 1920-1939
mydata$c1.League[mydata$ccode1==630 & mydata$year>1919 & mydata$year<1940]<- 1 # Iran 1920-1939
mydata$c1.League[mydata$ccode1==645 & mydata$year>1931 & mydata$year<1940]<- 1 # Iraq 1932-1939
mydata$c1.League[mydata$ccode1==205 & mydata$year>1920 & mydata$year<1940]<- 1 # Ireland gained independence from Britain in 1921.
mydata$c1.League[mydata$ccode1==325 & mydata$year>1919 & mydata$year<1938]<- 1 # Italy 1920-1937
mydata$c1.League[mydata$ccode1==740 & mydata$year>1919 & mydata$year<1934]<- 1 # Japan 1920-1933
mydata$c1.League[mydata$ccode1==367 & mydata$year>1920 & mydata$year<1940]<- 1 # Latvia 1921-1939
mydata$c1.League[mydata$ccode1==450 & mydata$year>1919 & mydata$year<1940]<- 1 # Liberia 1920-1939
mydata$c1.League[mydata$ccode1==368 & mydata$year>1920 & mydata$year<1940]<- 1 # Lithuania 1921-1939
mydata$c1.League[mydata$ccode1==212 & mydata$year>1919 & mydata$year<1940]<- 1 # Luxembourg 1920-1939
mydata$c1.League[mydata$ccode1==70 & mydata$year>1930 & mydata$year<1940]<- 1 # Mexico 1931-1939
mydata$c1.League[mydata$ccode1==210 & mydata$year>1919 & mydata$year<1940]<- 1 # Netherlands 1920-1939
mydata$c1.League[mydata$ccode1==920 & mydata$year>1919 & mydata$year<1940]<- 1 # New Zealand 1920-1939
mydata$c1.League[mydata$ccode1==93 & mydata$year>1919 & mydata$year<1937]<- 1 # Nicaragua 1920-1936
mydata$c1.League[mydata$ccode1==385 & mydata$year>1919 & mydata$year<1940]<- 1 # Norway 1920-1939
mydata$c1.League[mydata$ccode1==95 & mydata$year>1919 & mydata$year<1940]<- 1 # Panama 1920-1939
mydata$c1.League[mydata$ccode1==150 & mydata$year>1919 & mydata$year<1936]<- 1 # Paraguay 1920-1935
mydata$c1.League[mydata$ccode1==135 & mydata$year>1919 & mydata$year<1940]<- 1 # Peru 1920-1939
mydata$c1.League[mydata$ccode1==290 & mydata$year>1919 & mydata$year<1940]<- 1 # Poland 1920-1939
mydata$c1.League[mydata$ccode1==235 & mydata$year>1919 & mydata$year<1940]<- 1 # Portugal 1920-1939
mydata$c1.League[mydata$ccode1==360 & mydata$year>1919 & mydata$year<1941]<- 1 # Romania 1920-1940 ### NOTE LATE DATE
mydata$c1.League[mydata$ccode1==92 & mydata$year>1919 & mydata$year<1938]<- 1 # El Salvador 1920-1937
mydata$c1.League[mydata$ccode1==560 & mydata$year>1919 & mydata$year<1940]<- 1 # South Africa 1920-1939
mydata$c1.League[mydata$ccode1==230 & mydata$year>1919 & mydata$year<1940]<- 1 # Spain 1920-1939
mydata$c1.League[mydata$ccode1==380 & mydata$year>1919 & mydata$year<1940]<- 1 # Sweden 1920-1939
mydata$c1.League[mydata$ccode1==225 & mydata$year>1919 & mydata$year<1940]<- 1 # Switzerland 1920-1939
mydata$c1.League[mydata$ccode1==800 & mydata$year>1919 & mydata$year<1940]<- 1 # Thailand 1920-1939
mydata$c1.League[mydata$ccode1==640 & mydata$year>1931 & mydata$year<1940]<- 1 # Turkey 1932-1939
mydata$c1.League[mydata$ccode1==365 & mydata$year>1933 & mydata$year<1940]<- 1 # Russia for USSR 1934-1939 ### HOW TO HANDLE USSR? NO CCODE - JUST RUSSIA.
mydata$c1.League[mydata$ccode1==165 & mydata$year>1919 & mydata$year<1940]<- 1 # Uruguay 1920-1939
mydata$c1.League[mydata$ccode1==101 & mydata$year>1919 & mydata$year<1939]<- 1 # Venezuela 1920-1938
mydata$c1.League[mydata$ccode1==345 & mydata$year>1919 & mydata$year<1940]<- 1 # Yugoslavia 1920-1939

mydata$c2.League<- 0
mydata$c2.League[mydata$ccode2==700 & mydata$year>1933 & mydata$year<1940]<- 1 # Afghanistan 1934-1939
mydata$c2.League[mydata$ccode2==339 & mydata$year>1919 & mydata$year<1940]<- 1 # Albania 1920-1939(annexed by Italy April, 12 1939)
mydata$c2.League[mydata$ccode2==160 & mydata$year>1919 & mydata$year<1940]<- 1 # Argentina 1920-1939
mydata$c2.League[mydata$ccode2==900 & mydata$year>1919 & mydata$year<1940]<- 1 # Australia 1920-1939
mydata$c2.League[mydata$ccode2==305 & mydata$year>1919 & mydata$year<1939]<- 1 # Austria 1920-1939 (annexed by Germany April 10, 1938)
mydata$c2.League[mydata$ccode2==211 & mydata$year>1919 & mydata$year<1940]<- 1 # Belgium 1920-1939
mydata$c2.League[mydata$ccode2==145 & mydata$year>1919 & mydata$year<1940]<- 1 # Bolivia 1920-1939
mydata$c2.League[mydata$ccode2==140 & mydata$year>1919 & mydata$year<1927]<- 1 # Brazil 1920-1926
mydata$c2.League[mydata$ccode2==200 & mydata$year>1919 & mydata$year<1940]<- 1 # United Kingdom 1920-1939 # NOTE THAT BRITISH EMPIRE WAS THE SAME -- NEED TO ADD THEM. 
mydata$c2.League[mydata$ccode2==355 & mydata$year>1919 & mydata$year<1940]<- 1 # Bulgaria 1920-1939
mydata$c2.League[mydata$ccode2==20 & mydata$year>1919 & mydata$year<1940]<- 1 # Canada 1920-1939
mydata$c2.League[mydata$ccode2==155 & mydata$year>1919 & mydata$year<1939]<- 1 # Chile 1920-1938
mydata$c2.League[mydata$ccode2==710 & mydata$year>1919 & mydata$year<1940]<- 1 # China 1920-1939
mydata$c2.League[mydata$ccode2==100 & mydata$year>1919 & mydata$year<1940]<- 1 # Colombia 1920-1939
mydata$c2.League[mydata$ccode2==94 & mydata$year>1919 & mydata$year<1926]<- 1 # Costa Rica 1920-1925
mydata$c2.League[mydata$ccode2==40 & mydata$year>1919 & mydata$year<1940]<- 1 # Cuba 1920-1939
mydata$c2.League[mydata$ccode2==315 & mydata$year>1919 & mydata$year<1940]<- 1 # Czechoslovakia 1920-1939 (annexed by Germany March 15, 1939)
mydata$c2.League[mydata$ccode2==390 & mydata$year>1919 & mydata$year<1940]<- 1 # Denmark 1920-1939
mydata$c2.League[mydata$ccode2==42 & mydata$year>1923 & mydata$year<1940]<- 1 # Dominican Republic 1924-1939
mydata$c2.League[mydata$ccode2==130 & mydata$year>1933 & mydata$year<1940]<- 1 # Ecuador 1934-1939
mydata$c2.League[mydata$ccode2==651 & mydata$year>1936 & mydata$year<1940]<- 1 # Egypt 1937-1939
mydata$c2.League[mydata$ccode2==366 & mydata$year>1920 & mydata$year<1940]<- 1 # Estonia 1921-1939
mydata$c2.League[mydata$ccode2==530 & mydata$year>1922 & mydata$year<1937]<- 1 # Ethiopia 1923-1936 (annexed by Italy May 9, 1936)
mydata$c2.League[mydata$ccode2==375 & mydata$year>1919 & mydata$year<1940]<- 1 # Finland 1920-1939
mydata$c2.League[mydata$ccode2==220 & mydata$year>1919 & mydata$year<1942]<- 1 # France 1920-1941 ### WHY WAS THIS LATER? DID THEY JUST NOT FORMALLY WITHDRAW UNTIL THEN?
mydata$c2.League[mydata$ccode2==255 & mydata$year>1925 & mydata$year<1934]<- 1 # Germany 1926-1933
mydata$c2.League[mydata$ccode2==350 & mydata$year>1919 & mydata$year<1940]<- 1 # Greece 1920-1939
mydata$c2.League[mydata$ccode2==90 & mydata$year>1919 & mydata$year<1937]<- 1 # Guatemala 1920-1936
mydata$c2.League[mydata$ccode2==41 & mydata$year>1919 & mydata$year<1940]<- 1 # Haiti 1920-1942 ### CHECK THIS END DATE
mydata$c2.League[mydata$ccode2==91 & mydata$year>1919 & mydata$year<1937]<- 1 # Honduras 1920-1936
mydata$c2.League[mydata$ccode2==310 & mydata$year>1921 & mydata$year<1940]<- 1 # Hungary 1922-1939
mydata$c2.League[mydata$ccode2==750 & mydata$year>1919 & mydata$year<1940]<- 1 # India 1920-1939
mydata$c2.League[mydata$ccode2==630 & mydata$year>1919 & mydata$year<1940]<- 1 # Iran 1920-1939
mydata$c2.League[mydata$ccode2==645 & mydata$year>1931 & mydata$year<1940]<- 1 # Iraq 1932-1939
mydata$c2.League[mydata$ccode2==205 & mydata$year>1920 & mydata$year<1940]<- 1 # Ireland gained independence from Britain in 1921.
mydata$c2.League[mydata$ccode2==325 & mydata$year>1919 & mydata$year<1938]<- 1 # Italy 1920-1937
mydata$c2.League[mydata$ccode2==740 & mydata$year>1919 & mydata$year<1934]<- 1 # Japan 1920-1933
mydata$c2.League[mydata$ccode2==367 & mydata$year>1920 & mydata$year<1940]<- 1 # Latvia 1921-1939
mydata$c2.League[mydata$ccode2==450 & mydata$year>1919 & mydata$year<1940]<- 1 # Liberia 1920-1939
mydata$c2.League[mydata$ccode2==368 & mydata$year>1920 & mydata$year<1940]<- 1 # Lithuania 1921-1939
mydata$c2.League[mydata$ccode2==212 & mydata$year>1919 & mydata$year<1940]<- 1 # Luxembourg 1920-1939
mydata$c2.League[mydata$ccode2==70 & mydata$year>1930 & mydata$year<1940]<- 1 # Mexico 1931-1939
mydata$c2.League[mydata$ccode2==210 & mydata$year>1919 & mydata$year<1940]<- 1 # Netherlands 1920-1939
mydata$c2.League[mydata$ccode2==920 & mydata$year>1919 & mydata$year<1940]<- 1 # New Zealand 1920-1939
mydata$c2.League[mydata$ccode2==93 & mydata$year>1919 & mydata$year<1937]<- 1 # Nicaragua 1920-1936
mydata$c2.League[mydata$ccode2==385 & mydata$year>1919 & mydata$year<1940]<- 1 # Norway 1920-1939
mydata$c2.League[mydata$ccode2==95 & mydata$year>1919 & mydata$year<1940]<- 1 # Panama 1920-1939
mydata$c2.League[mydata$ccode2==150 & mydata$year>1919 & mydata$year<1936]<- 1 # Paraguay 1920-1935
mydata$c2.League[mydata$ccode2==135 & mydata$year>1919 & mydata$year<1940]<- 1 # Peru 1920-1939
mydata$c2.League[mydata$ccode2==290 & mydata$year>1919 & mydata$year<1940]<- 1 # Poland 1920-1939
mydata$c2.League[mydata$ccode2==235 & mydata$year>1919 & mydata$year<1940]<- 1 # Portugal 1920-1939
mydata$c2.League[mydata$ccode2==360 & mydata$year>1919 & mydata$year<1941]<- 1 # Romania 1920-1940 ### NOTE LATE DATE
mydata$c2.League[mydata$ccode2==92 & mydata$year>1919 & mydata$year<1938]<- 1 # El Salvador 1920-1937
mydata$c2.League[mydata$ccode2==560 & mydata$year>1919 & mydata$year<1940]<- 1 # South Africa 1920-1939
mydata$c2.League[mydata$ccode2==230 & mydata$year>1919 & mydata$year<1940]<- 1 # Spain 1920-1939
mydata$c2.League[mydata$ccode2==380 & mydata$year>1919 & mydata$year<1940]<- 1 # Sweden 1920-1939
mydata$c2.League[mydata$ccode2==225 & mydata$year>1919 & mydata$year<1940]<- 1 # Switzerland 1920-1939
mydata$c2.League[mydata$ccode2==800 & mydata$year>1919 & mydata$year<1940]<- 1 # Thailand 1920-1939
mydata$c2.League[mydata$ccode2==640 & mydata$year>1931 & mydata$year<1940]<- 1 # Turkey 1932-1939
mydata$c2.League[mydata$ccode2==365 & mydata$year>1933 & mydata$year<1940]<- 1 # Russia for USSR 1934-1939 ### HOW TO HANDLE USSR? NO CCODE - JUST RUSSIA.
mydata$c2.League[mydata$ccode2==165 & mydata$year>1919 & mydata$year<1940]<- 1 # Uruguay 1920-1939
mydata$c2.League[mydata$ccode2==101 & mydata$year>1919 & mydata$year<1939]<- 1 # Venezuela 1920-1938
mydata$c2.League[mydata$ccode2==345 & mydata$year>1919 & mydata$year<1940]<- 1 # Yugoslavia 1920-1939

# Post World War II Liberal Order from 1945 - 1991 (i.e., "First World" aligned against the Soviet Union) see: http://www.nato.int/cps/en/natohq/topics_52044.htm

# Below are NATO Members through 1991; states aligned with the West but not in NATO; and neutral states that are liberal, democratic. 

mydata$c1.PostWarLiberal<-0
mydata$c1.PostWarLiberal[mydata$ccode1==211 & mydata$year>1944 & mydata$year<1992]<- 1 # Belgium
mydata$c1.PostWarLiberal[mydata$ccode1==20 & mydata$year>1944 & mydata$year<1992]<- 1 # Canada
mydata$c1.PostWarLiberal[mydata$ccode1==390 & mydata$year>1944 & mydata$year<1992]<- 1 # Denmark
mydata$c1.PostWarLiberal[mydata$ccode1==220 & mydata$year>1944 & mydata$year<1992]<- 1 # France
mydata$c1.PostWarLiberal[mydata$ccode1==395 & mydata$year>1944 & mydata$year<1992]<- 1 # Iceland
mydata$c1.PostWarLiberal[mydata$ccode1==325 & mydata$year>1944 & mydata$year<1992]<- 1 # Italy
mydata$c1.PostWarLiberal[mydata$ccode1==212 & mydata$year>1944 & mydata$year<1992]<- 1 # Luxembourg
mydata$c1.PostWarLiberal[mydata$ccode1==210 & mydata$year>1944 & mydata$year<1992]<- 1 # Netherlands
mydata$c1.PostWarLiberal[mydata$ccode1==385 & mydata$year>1944 & mydata$year<1992]<- 1 # Norway
mydata$c1.PostWarLiberal[mydata$ccode1==235 & mydata$year>1944 & mydata$year<1992]<- 1 # Portugal
mydata$c1.PostWarLiberal[mydata$ccode1==200 & mydata$year>1944 & mydata$year<1992]<- 1 # United Kingdom
mydata$c1.PostWarLiberal[mydata$ccode1==2 & mydata$year>1944 & mydata$year<1992]<- 1 # United States
mydata$c1.PostWarLiberal[mydata$ccode1==350 & mydata$year>1951 & mydata$year<1992]<- 1 # Greece joined NATO in 1952
mydata$c1.PostWarLiberal[mydata$ccode1==640 & mydata$year>1951 & mydata$year<1992]<- 1 # Turkey joined NATO in 1952
mydata$c1.PostWarLiberal[mydata$ccode1==260 & mydata$year>1954 & mydata$year<1991]<- 1 # German Federal Republic joined NATO in 1955 and was a member through October 1990
mydata$c1.PostWarLiberal[mydata$ccode1==255 & mydata$year>1989 & mydata$year<1992]<- 1 # Unified Germany joined NATO in October 1990 ## note overlap with W. Germany in 1990.
mydata$c1.PostWarLiberal[mydata$ccode1==230 & mydata$year>1981 & mydata$year<1992]<- 1 # Spain joined NATO in 1982
mydata$c1.PostWarLiberal[mydata$ccode1==666 & mydata$year>1947 & mydata$year<1992]<- 1 # Israel founded in 1948
mydata$c1.PostWarLiberal[mydata$ccode1==740 & mydata$year>1951 & mydata$year<1992]<- 1 # Japan: US occupation ended in 1952. See: http://afe.easia.columbia.edu/special/japan_1900_occupation.htm
mydata$c1.PostWarLiberal[mydata$ccode1==732 & mydata$year>1947 & mydata$year<1992]<- 1 # Republic of Korea. US occupation ended in 1948. Note CCODE 732 is the ROK specific code. 
mydata$c1.PostWarLiberal[mydata$ccode1==900 & mydata$year>1944 & mydata$year<1992]<- 1 # Australia. Note that the ANZUS treaty started in 1951 but Australia was aligned with West prior. 
mydata$c1.PostWarLiberal[mydata$ccode1==920 & mydata$year>1944 & mydata$year<1992]<- 1 # New Zealand. Note that the ANZUS treaty started in 1951 but NZ was aligned with West prior. 
mydata$c1.PostWarLiberal[mydata$ccode1==305 & mydata$year>1944 & mydata$year<1992]<- 1 # Austria was western, liberal democratic but neutral. Note CCODE 305 is the Austria-specific code. There is a separate Austria-Hungary code (300)
mydata$c1.PostWarLiberal[mydata$ccode1==205 & mydata$year>1944 & mydata$year<1992]<- 1 # Ireland was western, liberal democratic but neutral.
mydata$c1.PostWarLiberal[mydata$ccode1==380 & mydata$year>1944 & mydata$year<1992]<- 1 # Sweden was western, liberal democratic but neutral.
mydata$c1.PostWarLiberal[mydata$ccode1==225 & mydata$year>1944 & mydata$year<1992]<- 1 # Switzerland was western, liberal democratic but neutral.
mydata$c1.PostWarLiberal[mydata$ccode1==817 & mydata$year>1954 & mydata$year<1976]<- 1 # Republic of ("South") Vietnam as of 1955.
mydata$c1.PostWarLiberal[mydata$ccode1==713 & mydata$year>1946 & mydata$year<1992]<- 1 # Republic of China ("Taiwan") concluded its consitution in 1947. 


mydata$c2.PostWarLiberal<-0
mydata$c2.PostWarLiberal[mydata$ccode2==211 & mydata$year>1944 & mydata$year<1992]<- 1 # Belgium
mydata$c2.PostWarLiberal[mydata$ccode2==20 & mydata$year>1944 & mydata$year<1992]<- 1 # Canada
mydata$c2.PostWarLiberal[mydata$ccode2==390 & mydata$year>1944 & mydata$year<1992]<- 1 # Denmark
mydata$c2.PostWarLiberal[mydata$ccode2==220 & mydata$year>1944 & mydata$year<1992]<- 1 # France
mydata$c2.PostWarLiberal[mydata$ccode2==395 & mydata$year>1944 & mydata$year<1992]<- 1 # Iceland
mydata$c2.PostWarLiberal[mydata$ccode2==325 & mydata$year>1944 & mydata$year<1992]<- 1 # Italy
mydata$c2.PostWarLiberal[mydata$ccode2==212 & mydata$year>1944 & mydata$year<1992]<- 1 # Luxembourg
mydata$c2.PostWarLiberal[mydata$ccode2==210 & mydata$year>1944 & mydata$year<1992]<- 1 # Netherlands
mydata$c2.PostWarLiberal[mydata$ccode2==385 & mydata$year>1944 & mydata$year<1992]<- 1 # Norway
mydata$c2.PostWarLiberal[mydata$ccode2==235 & mydata$year>1944 & mydata$year<1992]<- 1 # Portugal
mydata$c2.PostWarLiberal[mydata$ccode2==200 & mydata$year>1944 & mydata$year<1992]<- 1 # United Kingdom
mydata$c2.PostWarLiberal[mydata$ccode2==2 & mydata$year>1944 & mydata$year<1992]<- 1 # United States
mydata$c2.PostWarLiberal[mydata$ccode2==350 & mydata$year>1951 & mydata$year<1992]<- 1 # Greece joined NATO in 1952
mydata$c2.PostWarLiberal[mydata$ccode2==640 & mydata$year>1951 & mydata$year<1992]<- 1 # Turkey joined NATO in 1952
mydata$c2.PostWarLiberal[mydata$ccode2==260 & mydata$year>1954 & mydata$year<1991]<- 1 # German Federal Republic joined NATO in 1955 and was a member through October 1990
mydata$c2.PostWarLiberal[mydata$ccode2==255 & mydata$year>1989 & mydata$year<1992]<- 1 # Unified Germany joined NATO in October 1990 ## note overlap with W. Germany in 1990.
mydata$c2.PostWarLiberal[mydata$ccode2==230 & mydata$year>1981 & mydata$year<1992]<- 1 # Spain joined NATO in 1982
mydata$c2.PostWarLiberal[mydata$ccode2==666 & mydata$year>1947 & mydata$year<1992]<- 1 # Israel founded in 1948
mydata$c2.PostWarLiberal[mydata$ccode2==740 & mydata$year>1951 & mydata$year<1992]<- 1 # Japan: US occupation ended in 1952. See: http://afe.easia.columbia.edu/special/japan_1900_occupation.htm
mydata$c2.PostWarLiberal[mydata$ccode2==732 & mydata$year>1947 & mydata$year<1992]<- 1 # Republic of Korea. US occupation ended in 1948. Note CCODE 732 is the ROK specific code.
mydata$c2.PostWarLiberal[mydata$ccode2==900 & mydata$year>1944 & mydata$year<1992]<- 1 # Australia. Note that the ANZUS treaty started in 1951 but Australia was aligned with West prior. 
mydata$c2.PostWarLiberal[mydata$ccode2==920 & mydata$year>1944 & mydata$year<1992]<- 1 # New Zealand. Note that the ANZUS treaty started in 1951 but NZ was aligned with West prior. 
mydata$c2.PostWarLiberal[mydata$ccode2==305 & mydata$year>1944 & mydata$year<1992]<- 1 # Austria was western, liberal democratic but neutral. Note CCODE 305 is the Austria-specific code. There is a separate Austria-Hungary code (300)
mydata$c2.PostWarLiberal[mydata$ccode2==205 & mydata$year>1944 & mydata$year<1992]<- 1 # Ireland was western, liberal democratic but neutral.
mydata$c2.PostWarLiberal[mydata$ccode2==380 & mydata$year>1944 & mydata$year<1992]<- 1 # Sweden was western, liberal democratic but neutral.
mydata$c2.PostWarLiberal[mydata$ccode2==225 & mydata$year>1944 & mydata$year<1992]<- 1 # Switzerland was western, liberal democratic but neutral.
mydata$c2.PostWarLiberal[mydata$ccode2==817 & mydata$year>1954 & mydata$year<1976]<- 1 # Republic of ("South") Vietnam as of 1955. 
mydata$c2.PostWarLiberal[mydata$ccode2==713 & mydata$year>1946 & mydata$year<1992]<- 1 # Republic of China ("Taiwan") concluded its consitution in 1947. 


# Post World War II Communist Order from 1945 - 1991 (i.e., "Second World" aligned against NATO) See: https://history.state.gov/milestones/1953-1960/warsaw-treaty 
# for Warsaw Pact countries.  Note that Soviet states do not have individual year dyad information in the dataset. 

# Below are Warsaw Pact States, other communist states, and states aligned with the Soviet sphere but not formally allied. 

mydata$c1.PostWarCommunist<-0
mydata$c1.PostWarCommunist[mydata$ccode1==339 & mydata$year>1954 & mydata$year<1992]<- 1 # Albania
mydata$c1.PostWarCommunist[mydata$ccode1==100 & mydata$year>1954 & mydata$year<1992]<- 1 # Bulgaria
mydata$c1.PostWarCommunist[mydata$ccode1==315 & mydata$year>1954 & mydata$year<1992]<- 1 # Czechoslovakia
mydata$c1.PostWarCommunist[mydata$ccode1==265 & mydata$year>1954 & mydata$year<1991]<- 1 # German Democratic Republic through 1990
mydata$c1.PostWarCommunist[mydata$ccode1==310 & mydata$year>1954 & mydata$year<1992]<- 1 # Hungary
mydata$c1.PostWarCommunist[mydata$ccode1==290 & mydata$year>1954 & mydata$year<1992]<- 1 # Poland
mydata$c1.PostWarCommunist[mydata$ccode1==360 & mydata$year>1954 & mydata$year<1992]<- 1 # Romania
mydata$c1.PostWarCommunist[mydata$ccode1==365 & mydata$year>1954 & mydata$year<1992]<- 1 # Russia
# mydata$c1.PostWarCommunist[mydata$ccode1==375 & mydata$year>1944 & mydata$year<1992]<- 1 # Finland was neutral but leaned toward the Soviets in the latter part of the Cold War. 
mydata$c1.PostWarCommunist[mydata$ccode1==710 & mydata$year>1948 & mydata$year<1992]<- 1 # People's Republic of China founded in 1949
mydata$c1.PostWarCommunist[mydata$ccode1==731 & mydata$year>1947 & mydata$year<1992]<- 1 # Democratic People's Republic of Korea founded in 1948. Previously Soviet occupied.
mydata$c1.PostWarCommunist[mydata$ccode1==812 & mydata$year>1948 & mydata$year<1992]<- 1 # Lao Peoples Democratic Republic gained independence in 1949.
mydata$c1.PostWarCommunist[mydata$ccode1==712 & mydata$year>1944 & mydata$year<1992]<- 1 # Mongolia
mydata$c1.PostWarCommunist[mydata$ccode1==816 & mydata$year>1944 & mydata$year<1992]<- 1 # Socialist Republic of Vietnam gained independence in 1945.
mydata$c1.PostWarCommunist[mydata$ccode1==371 & mydata$year>1944 & mydata$year<1992]<- 1 # Armenia was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==373 & mydata$year>1944 & mydata$year<1992]<- 1 # Azerbaijan was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==370 & mydata$year>1944 & mydata$year<1992]<- 1 # Belarus was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==366 & mydata$year>1944 & mydata$year<1992]<- 1 # Estonia was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==372 & mydata$year>1944 & mydata$year<1992]<- 1 # Georgia was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==705 & mydata$year>1944 & mydata$year<1992]<- 1 # Kazakhstan was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==703 & mydata$year>1944 & mydata$year<1992]<- 1 # Kyrgyzstan was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==367 & mydata$year>1944 & mydata$year<1992]<- 1 # Latvia was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==368 & mydata$year>1944 & mydata$year<1992]<- 1 # Lithuania was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==359 & mydata$year>1944 & mydata$year<1992]<- 1 # Moldova was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==702 & mydata$year>1944 & mydata$year<1992]<- 1 # Tajikistan was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==701 & mydata$year>1944 & mydata$year<1992]<- 1 # Turkmenistan was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==369 & mydata$year>1944 & mydata$year<1992]<- 1 # Ukraine was part of the Soviet Union.
mydata$c1.PostWarCommunist[mydata$ccode1==704 & mydata$year>1944 & mydata$year<1992]<- 1 # Uzbekistan was part of the Soviet Union.

mydata$c2.PostWarCommunist<-0
mydata$c2.PostWarCommunist[mydata$ccode2==339 & mydata$year>1954 & mydata$year<1992]<- 1 # Albania
mydata$c2.PostWarCommunist[mydata$ccode2==100 & mydata$year>1954 & mydata$year<1992]<- 1 # Bulgaria
mydata$c2.PostWarCommunist[mydata$ccode2==315 & mydata$year>1954 & mydata$year<1992]<- 1 # Czechoslovakia
mydata$c2.PostWarCommunist[mydata$ccode2==265 & mydata$year>1954 & mydata$year<1991]<- 1 # German Democratic Republic through 1990
mydata$c2.PostWarCommunist[mydata$ccode2==310 & mydata$year>1954 & mydata$year<1992]<- 1 # Hungary
mydata$c2.PostWarCommunist[mydata$ccode2==290 & mydata$year>1954 & mydata$year<1992]<- 1 # Poland
mydata$c2.PostWarCommunist[mydata$ccode2==360 & mydata$year>1954 & mydata$year<1992]<- 1 # Romania
mydata$c2.PostWarCommunist[mydata$ccode2==365 & mydata$year>1954 & mydata$year<1992]<- 1 # Russia
# mydata$c2.PostWarCommunist[mydata$ccode2==375 & mydata$year>1944 & mydata$year<1992]<- 1 # Finland was neutral but leaned toward the Soviets in the latter part of the Cold War. 
mydata$c2.PostWarCommunist[mydata$ccode2==710 & mydata$year>1948 & mydata$year<1992]<- 1 # People's Republic of China founded in 1949
mydata$c2.PostWarCommunist[mydata$ccode2==731 & mydata$year>1947 & mydata$year<1992]<- 1 # Democratic People's Republic of Korea founded in 1948. Previously Soviet occupied.
mydata$c2.PostWarCommunist[mydata$ccode2==812 & mydata$year>1948 & mydata$year<1992]<- 1 # Lao Peoples Democratic Republic gained independence in 1949.
mydata$c2.PostWarCommunist[mydata$ccode2==712 & mydata$year>1944 & mydata$year<1992]<- 1 # Mongolia
mydata$c2.PostWarCommunist[mydata$ccode2==816 & mydata$year>1944 & mydata$year<1992]<- 1 # Socialist Republic of Vietnam gained independence in 1945.
mydata$c2.PostWarCommunist[mydata$ccode2==371 & mydata$year>1944 & mydata$year<1992]<- 1 # Armenia was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==373 & mydata$year>1944 & mydata$year<1992]<- 1 # Azerbaijan was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==370 & mydata$year>1944 & mydata$year<1992]<- 1 # Belarus was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==366 & mydata$year>1944 & mydata$year<1992]<- 1 # Estonia was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==372 & mydata$year>1944 & mydata$year<1992]<- 1 # Georgia was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==705 & mydata$year>1944 & mydata$year<1992]<- 1 # Kazakhstan was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==703 & mydata$year>1944 & mydata$year<1992]<- 1 # Kyrgyzstan was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==367 & mydata$year>1944 & mydata$year<1992]<- 1 # Latvia was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==368 & mydata$year>1944 & mydata$year<1992]<- 1 # Lithuania was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==359 & mydata$year>1944 & mydata$year<1992]<- 1 # Moldova was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==702 & mydata$year>1944 & mydata$year<1992]<- 1 # Tajikistan was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==701 & mydata$year>1944 & mydata$year<1992]<- 1 # Turkmenistan was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==369 & mydata$year>1944 & mydata$year<1992]<- 1 # Ukraine was part of the Soviet Union.
mydata$c2.PostWarCommunist[mydata$ccode2==704 & mydata$year>1944 & mydata$year<1992]<- 1 # Uzbekistan was part of the Soviet Union.

# Warsaw Pact countries
mydata$c1.WarsawPact<-0
mydata$c1.WarsawPact[mydata$ccode1==339 & mydata$year>1954 & mydata$year<1969]<- 1 # Albania through 1968
mydata$c1.WarsawPact[mydata$ccode1==100 & mydata$year>1954 & mydata$year<1992]<- 1 # Bulgaria
mydata$c1.WarsawPact[mydata$ccode1==315 & mydata$year>1954 & mydata$year<1992]<- 1 # Czechoslovakia
mydata$c1.WarsawPact[mydata$ccode1==265 & mydata$year>1954 & mydata$year<1991]<- 1 # German Democratic Republic through 1990
mydata$c1.WarsawPact[mydata$ccode1==310 & mydata$year>1954 & mydata$year<1992]<- 1 # Hungary
mydata$c1.WarsawPact[mydata$ccode1==290 & mydata$year>1954 & mydata$year<1992]<- 1 # Poland
mydata$c1.WarsawPact[mydata$ccode1==360 & mydata$year>1954 & mydata$year<1992]<- 1 # Romania
mydata$c1.WarsawPact[mydata$ccode1==365 & mydata$year>1954 & mydata$year<1992]<- 1 # Russia

mydata$c2.WarsawPact<-0
mydata$c2.WarsawPact[mydata$ccode2==339 & mydata$year>1954 & mydata$year<1969]<- 1 # Albania through 1968
mydata$c2.WarsawPact[mydata$ccode2==100 & mydata$year>1954 & mydata$year<1992]<- 1 # Bulgaria
mydata$c2.WarsawPact[mydata$ccode2==315 & mydata$year>1954 & mydata$year<1992]<- 1 # Czechoslovakia
mydata$c2.WarsawPact[mydata$ccode2==265 & mydata$year>1954 & mydata$year<1991]<- 1 # German Democratic Republic through 1990
mydata$c2.WarsawPact[mydata$ccode2==310 & mydata$year>1954 & mydata$year<1992]<- 1 # Hungary
mydata$c2.WarsawPact[mydata$ccode2==290 & mydata$year>1954 & mydata$year<1992]<- 1 # Poland
mydata$c2.WarsawPact[mydata$ccode2==360 & mydata$year>1954 & mydata$year<1992]<- 1 # Romania
mydata$c2.WarsawPact[mydata$ccode2==365 & mydata$year>1954 & mydata$year<1992]<- 1 # Russia


# Post World War II states that were in neither camp.  This includes both un-aligned middle powers and those states previously referred to as the "third world." 

mydata$c1.PostWarOther<-0
mydata$c1.PostWarOther[mydata$c1.PostWarCommunist==0 & mydata$c1.PostWarLiberal==0 & mydata$year>1944 & mydata$year<1992]<-1

mydata$c2.PostWarOther<-0
mydata$c2.PostWarOther[mydata$c2.PostWarCommunist==0 & mydata$c2.PostWarLiberal==0 & mydata$year>1944 & mydata$year<1992]<-1

# Post-Cold War Western order (assumed to be end-of-CW coalition)

mydata$c1.PostCWLiberal<-0
mydata$c1.PostCWLiberal[mydata$ccode1==211 & mydata$year>1991]<- 1 # Belgium
mydata$c1.PostCWLiberal[mydata$ccode1==20 & mydata$year>1991]<- 1 # Canada
mydata$c1.PostCWLiberal[mydata$ccode1==390 & mydata$year>1991]<- 1 # Denmark
mydata$c1.PostCWLiberal[mydata$ccode1==220 & mydata$year>1991]<- 1 # France
mydata$c1.PostCWLiberal[mydata$ccode1==395 & mydata$year>1991]<- 1 # Iceland
mydata$c1.PostCWLiberal[mydata$ccode1==325 & mydata$year>1991]<- 1 # Italy
mydata$c1.PostCWLiberal[mydata$ccode1==212 & mydata$year>1991]<- 1 # Luxembourg
mydata$c1.PostCWLiberal[mydata$ccode1==210 & mydata$year>1991]<- 1 # Netherlands
mydata$c1.PostCWLiberal[mydata$ccode1==385 & mydata$year>1991]<- 1 # Norway
mydata$c1.PostCWLiberal[mydata$ccode1==235 & mydata$year>1991]<- 1 # Portugal
mydata$c1.PostCWLiberal[mydata$ccode1==200 & mydata$year>1991]<- 1 # United Kingdom
mydata$c1.PostCWLiberal[mydata$ccode1==2 & mydata$year>1991]<- 1 # United States
mydata$c1.PostCWLiberal[mydata$ccode1==350 & mydata$year>1991]<- 1 # Greece joined NATO in 1952
mydata$c1.PostCWLiberal[mydata$ccode1==640 & mydata$year>1991]<- 1 # Turkey joined NATO in 1952
mydata$c1.PostCWLiberal[mydata$ccode1==260 & mydata$year>1991]<- 1 # German Federal Republic joined NATO in 1955 and was a member through October 1990
mydata$c1.PostCWLiberal[mydata$ccode1==255 & mydata$year>1991]<- 1 # Unified Germany joined NATO in October 1990 ## note overlap with W. Germany in 1990.
mydata$c1.PostCWLiberal[mydata$ccode1==230 & mydata$year>1991]<- 1 # Spain joined NATO in 1982
mydata$c1.PostCWLiberal[mydata$ccode1==666 & mydata$year>1991]<- 1 # Israel founded in 1948
mydata$c1.PostCWLiberal[mydata$ccode1==740 & mydata$year>1991]<- 1 # Japan: US occupation ended in 1952. See: http://afe.easia.columbia.edu/special/japan_1900_occupation.htm
mydata$c1.PostCWLiberal[mydata$ccode1==732 & mydata$year>1991]<- 1 # Republic of Korea. US occupation ended in 1948. Note CCODE 732 is the ROK specific code.
mydata$c1.PostCWLiberal[mydata$ccode1==900 & mydata$year>1991]<- 1 # Australia. Note that the ANZUS treaty started in 1951 but Australia was aligned with West prior. 
mydata$c1.PostCWLiberal[mydata$ccode1==920 & mydata$year>1991]<- 1 # New Zealand. Note that the ANZUS treaty started in 1951 but NZ was aligned with West prior. 
mydata$c1.PostCWLiberal[mydata$ccode1==305 & mydata$year>1991]<- 1 # Austria was western, liberal democratic but neutral. Note CCODE 305 is the Austria-specific code. There is a separate Austria-Hungary code (300)
mydata$c1.PostCWLiberal[mydata$ccode1==205 & mydata$year>1991]<- 1 # Ireland was western, liberal democratic but neutral.
mydata$c1.PostCWLiberal[mydata$ccode1==380 & mydata$year>1991]<- 1 # Sweden was western, liberal democratic but neutral.
mydata$c1.PostCWLiberal[mydata$ccode1==225 & mydata$year>1991]<- 1 # Switzerland was western, liberal democratic but neutral.
mydata$c1.PostCWLiberal[mydata$ccode1==713 & mydata$year>1991]<- 1 # Republic of China ("Taiwan") concluded its consitution in 1947. 



mydata$c2.PostCWLiberal<-0
mydata$c2.PostCWLiberal[mydata$ccode2==211 & mydata$year>1991]<- 1 # Belgium
mydata$c2.PostCWLiberal[mydata$ccode2==20 & mydata$year>1991]<- 1 # Canada
mydata$c2.PostCWLiberal[mydata$ccode2==390 & mydata$year>1991]<- 1 # Denmark
mydata$c2.PostCWLiberal[mydata$ccode2==220 & mydata$year>1991]<- 1 # France
mydata$c2.PostCWLiberal[mydata$ccode2==395 & mydata$year>1991]<- 1 # Iceland
mydata$c2.PostCWLiberal[mydata$ccode2==325 & mydata$year>1991]<- 1 # Italy
mydata$c2.PostCWLiberal[mydata$ccode2==212 & mydata$year>1991]<- 1 # Luxembourg
mydata$c2.PostCWLiberal[mydata$ccode2==210 & mydata$year>1991]<- 1 # Netherlands
mydata$c2.PostCWLiberal[mydata$ccode2==385 & mydata$year>1991]<- 1 # Norway
mydata$c2.PostCWLiberal[mydata$ccode2==235 & mydata$year>1991]<- 1 # Portugal
mydata$c2.PostCWLiberal[mydata$ccode2==200 & mydata$year>1991]<- 1 # United Kingdom
mydata$c2.PostCWLiberal[mydata$ccode2==2 & mydata$year>1991]<- 1 # United States
mydata$c2.PostCWLiberal[mydata$ccode2==350 & mydata$year>1991]<- 1 # Greece joined NATO in 1952
mydata$c2.PostCWLiberal[mydata$ccode2==640 & mydata$year>1991]<- 1 # Turkey joined NATO in 1952
mydata$c2.PostCWLiberal[mydata$ccode2==260 & mydata$year>1991]<- 1 # German Federal Republic joined NATO in 1955 and was a member through October 1990
mydata$c2.PostCWLiberal[mydata$ccode2==255 & mydata$year>1991]<- 1 # Unified Germany joined NATO in October 1990 ## note overlap with W. Germany in 1990.
mydata$c2.PostCWLiberal[mydata$ccode2==230 & mydata$year>1991]<- 1 # Spain joined NATO in 1982
mydata$c2.PostCWLiberal[mydata$ccode2==666 & mydata$year>1991]<- 1 # Israel founded in 1948
mydata$c2.PostCWLiberal[mydata$ccode2==740 & mydata$year>1991]<- 1 # Japan: US occupation ended in 1952. See: http://afe.easia.columbia.edu/special/japan_1900_occupation.htm
mydata$c2.PostCWLiberal[mydata$ccode2==732 & mydata$year>1991]<- 1 # Republic of Korea. US occupation ended in 1948. Note CCODE 732 is the ROK specific code.
mydata$c2.PostCWLiberal[mydata$ccode2==900 & mydata$year>1991]<- 1 # Australia. Note that the ANZUS treaty started in 1951 but Australia was aligned with West prior. 
mydata$c2.PostCWLiberal[mydata$ccode2==920 & mydata$year>1991]<- 1 # New Zealand. Note that the ANZUS treaty started in 1951 but NZ was aligned with West prior. 
mydata$c2.PostCWLiberal[mydata$ccode2==305 & mydata$year>1991]<- 1 # Austria was western, liberal democratic but neutral. Note CCODE 305 is the Austria-specific code. There is a separate Austria-Hungary code (300)
mydata$c2.PostCWLiberal[mydata$ccode2==205 & mydata$year>1991]<- 1 # Ireland was western, liberal democratic but neutral.
mydata$c2.PostCWLiberal[mydata$ccode2==380 & mydata$year>1991]<- 1 # Sweden was western, liberal democratic but neutral.
mydata$c2.PostCWLiberal[mydata$ccode2==225 & mydata$year>1991]<- 1 # Switzerland was western, liberal democratic but neutral.
mydata$c2.PostCWLiberal[mydata$ccode2==713 & mydata$year>1991]<- 1 # Republic of China ("Taiwan") concluded its consitution in 1947. 

# NATO expansion countries; coding based on status as of 1/1/year
mydata$c1.PostCWLiberal[mydata$ccode1==316 & mydata$year>1999]<- 1 # Czech Republic
mydata$c1.PostCWLiberal[mydata$ccode1==290 & mydata$year>1999]<- 1 # Poland
mydata$c1.PostCWLiberal[mydata$ccode1==310 & mydata$year>1999]<- 1 # Hungary
mydata$c1.PostCWLiberal[mydata$ccode1==355 & mydata$year>2004]<- 1 # Bulgaria
mydata$c1.PostCWLiberal[mydata$ccode1==366 & mydata$year>2004]<- 1 # Estonia
mydata$c1.PostCWLiberal[mydata$ccode1==367 & mydata$year>2004]<- 1 # Latvia
mydata$c1.PostCWLiberal[mydata$ccode1==368 & mydata$year>2004]<- 1 # Lithuania
mydata$c1.PostCWLiberal[mydata$ccode1==360 & mydata$year>2004]<- 1 # Romania
mydata$c1.PostCWLiberal[mydata$ccode1==317 & mydata$year>2004]<- 1 # Slovakia
mydata$c1.PostCWLiberal[mydata$ccode1==349 & mydata$year>2004]<- 1 # Slovenia
mydata$c1.PostCWLiberal[mydata$ccode1==339 & mydata$year>2009]<- 1 # Albania
mydata$c1.PostCWLiberal[mydata$ccode1==344 & mydata$year>2009]<- 1 # Croatia
mydata$c2.PostCWLiberal[mydata$ccode2==316 & mydata$year>1999]<- 1 # Czech Republic
mydata$c2.PostCWLiberal[mydata$ccode2==290 & mydata$year>1999]<- 1 # Poland
mydata$c2.PostCWLiberal[mydata$ccode2==310 & mydata$year>1999]<- 1 # Hungary
mydata$c2.PostCWLiberal[mydata$ccode2==355 & mydata$year>2004]<- 1 # Bulgaria
mydata$c2.PostCWLiberal[mydata$ccode2==366 & mydata$year>2004]<- 1 # Estonia
mydata$c2.PostCWLiberal[mydata$ccode2==367 & mydata$year>2004]<- 1 # Latvia
mydata$c2.PostCWLiberal[mydata$ccode2==368 & mydata$year>2004]<- 1 # Lithuania
mydata$c2.PostCWLiberal[mydata$ccode2==360 & mydata$year>2004]<- 1 # Romania
mydata$c2.PostCWLiberal[mydata$ccode2==317 & mydata$year>2004]<- 1 # Slovakia
mydata$c2.PostCWLiberal[mydata$ccode2==349 & mydata$year>2004]<- 1 # Slovenia
mydata$c2.PostCWLiberal[mydata$ccode2==339 & mydata$year>2009]<- 1 # Albania
mydata$c2.PostCWLiberal[mydata$ccode2==344 & mydata$year>2009]<- 1 # Croatia
# mydata$c2.PostCWLiberal[mydata$ccode2==341 & mydata$year>2017]<- 1 # Montenegro

# EU expansion (not including those predated by NATO expansion); 1/1/year
mydata$c1.PostCWLiberal[mydata$ccode1==375 & mydata$year>1994]<- 1 # Finland
mydata$c1.PostCWLiberal[mydata$ccode1==352 & mydata$year>2004]<- 1 # Cyprus
mydata$c1.PostCWLiberal[mydata$ccode1==338 & mydata$year>2004]<- 1 # Malta
mydata$c2.PostCWLiberal[mydata$ccode2==375 & mydata$year>1994]<- 1 # Finland
mydata$c2.PostCWLiberal[mydata$ccode2==352 & mydata$year>2004]<- 1 # Cyprus
mydata$c2.PostCWLiberal[mydata$ccode2==338 & mydata$year>2004]<- 1 # Malta


mydata$order <- 0  
mydata$order[(mydata$c1.concert==1 & mydata$c2.concert==1) | (mydata$c1.bismarck==1 & mydata$c2.bismarck==1) | (mydata$c1.League==1 & mydata$c2.League==1) | (mydata$c1.PostWarLiberal==1 & mydata$c2.PostWarLiberal==1) | (mydata$c1.PostWarCommunist==1 & mydata$c2.PostWarCommunist==1) | (mydata$c1.PostCWLiberal==1 & mydata$c2.PostCWLiberal==1)] <- 1

mydata$outsideorder <- 0  
mydata$outsideorder[(mydata$c1.concert==1 & mydata$c2.concert==0) | (mydata$c1.bismarck==1 & mydata$c2.bismarck==0) | (mydata$c1.League==1 & mydata$c2.League==0 & !(mydata$ccode2 %in% c(255,365,740))) | (mydata$c1.concert==0 & mydata$c2.concert==1) | (mydata$c1.bismarck==0 & mydata$c2.bismarck==1) | (mydata$c1.League==0 & !(mydata$ccode1 %in% c(255,365,740)) & mydata$c2.League==1) | (mydata$c1.PostCWLiberal==1 & mydata$c2.PostCWLiberal==0) | (mydata$c1.PostCWLiberal==0 & mydata$c2.PostCWLiberal==1)] <- 1

mydata$betweenorders <- 0
mydata$betweenorders[(mydata$c1.League==1 & mydata$ccode2 %in% c(255,365,740)) | (mydata$ccode1 %in% c(255,365,740) & mydata$c2.League==1) | (mydata$c1.PostWarLiberal==1 & mydata$c2.PostWarLiberal==0) | (mydata$c1.PostWarLiberal==0 & mydata$c2.PostWarLiberal==1) | (mydata$c1.PostWarCommunist==1 & mydata$c2.PostWarCommunist==0) | (mydata$c1.PostWarCommunist==0 & mydata$c2.PostWarCommunist==1)] <- 1
mydata.nona <- na.omit(mydata)


# Next up, we bootstrap confidence intervals for the rate of conflict initation for various groups of states.

library(boot)

samplemean <- function(x, d) {
  return(mean(x[d]))
}

conditions <- mydata.nona$c1.concert==1 & mydata.nona$c2.concert==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500, parallel="snow")
rates <- data.frame(matrix(c("Concert of Europe", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975))), ncol=4), stringsAsFactors=FALSE)
names(rates) <- c("Order", "Rate", "pct95lo", "pct95hi")
gc()

conditions <- mydata.nona$c1.interim==1 & mydata.nona$c2.interim==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Mid-19th cen. Europe", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$c1.bismarck==1 & mydata.nona$c2.bismarck==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Bismarck", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$c1.wilhelm==1 & mydata.nona$c2.wilhelm==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Wilhelm", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$c1.League==1 & mydata.nona$c2.League==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("League of Nations", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- (mydata.nona$c1.League==1 & mydata.nona$ccode2 %in% c(255,365,740)) | (mydata.nona$ccode2 %in% c(255,740) & mydata.nona$c2.League==1)
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("League v. Germany/USSR/Japan", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$c1.PostWarLiberal==1 & mydata.nona$c2.PostWarLiberal==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Post-WWII Liberal", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- ((mydata.nona$c1.PostWarLiberal==1 & mydata.nona$c2.PostWarLiberal==0) | (mydata.nona$c1.PostWarLiberal==0 & mydata.nona$c2.PostWarLiberal==1))
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Post-WWII Liberal vs. Other", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$c1.PostWarCommunist==1 & mydata.nona$c2.PostWarCommunist==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Post-WWII Communist", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- ((mydata.nona$c1.PostWarCommunist==1 & mydata.nona$c2.PostWarCommunist==0) | (mydata.nona$c1.PostWarCommunist==0 & mydata.nona$c2.PostWarCommunist==1))
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Post-WWII Communist vs. Other", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$c1.PostWarOther==1 & mydata.nona$c2.PostWarOther==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Post-WWII Intra-Third World", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$c1.PostCWLiberal==1 & mydata.nona$c2.PostCWLiberal==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Post-Cold War Liberal", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- ((mydata.nona$c1.PostCWLiberal==1 & mydata.nona$c2.PostCWLiberal==0) | (mydata.nona$c1.PostCWLiberal==0 & mydata.nona$c2.PostCWLiberal==1))
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Post-Cold War Liberal vs. Other", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$c1.PostCWLiberal==0 & mydata.nona$c2.PostCWLiberal==0 & mydata.nona$year>1991
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Post-Cold War other", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$order==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Any Order", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$outsideorder==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Cross-Order", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- mydata.nona$betweenorders==1
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("Between Orders", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

conditions <- (mydata.nona$order==0 & mydata.nona$outsideorder==0 & mydata.nona$betweenorders==0)
myboot <- boot(data=((mydata.nona$force[conditions])/(mydata.nona$relevance.BC[conditions])), statistic=samplemean, R=2500)
rates[nrow(rates)+1,] <- c("No Order", myboot$t0, quantile(myboot$t, probs=c(0.025, 0.975)))
gc()

# What are these post-CW "liberal vs. other" conflicts?
conditions <- ((mydata.nona$c1.PostCWLiberal==1 & mydata.nona$c2.PostCWLiberal==0) | (mydata.nona$c1.PostCWLiberal==0 & mydata.nona$c2.PostCWLiberal==1))
mydata.nona[(conditions==1 & mydata.nona$force==1),c(1:5,17)]

system("say Bootstrapping complete!")

# This gives us rates and confidence intervals, which we can save in compact form for easy plotting.

rates$Rate <- as.numeric(rates$Rate)
rates$pct95lo <- as.numeric(rates$pct95lo)
rates$pct95hi <- as.numeric(rates$pct95hi)

rates$Order[7:14] <- c("Western Liberal", "Western Liberal vs. Other", "Communist", "Communist vs. Other", "Other", "Western Liberal", "Western Liberal vs. Other", "Other")

order.cols <- c("dodgerblue4", "firebrick4", "dodgerblue4", "firebrick4", "dodgerblue4", "firebrick4", "dodgerblue4", "firebrick4", "dodgerblue4", "firebrick4", "dodgerblue4", "dodgerblue4", "firebrick4", "dodgerblue4" )

par(mar=c(4,7,1,1))
plot(NA, xlim=c(0,max(rates$pct95hi)), ylim=c(0.5,17.5), axes=FALSE, ylab="", xlab="Rate of Conflict Initiation", cex.lab=0.8)
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], border=NA, col="grey92")
axis(1, at=seq(0,0.12, by=0.01), cex.axis=0.8, tick=FALSE)
axis(2, at=c(17:1), labels=c(rates$Order[1:4], " ", rates$Order[5:6], " ", rates$Order[7:11], " ", rates$Order[12:14]), cex.axis=0.4, tick=FALSE, las=1)

print.row <- c(17:14, 12:11, 9:5, 3:1)
for(i in 1:14){
	row <- print.row[i]
	segments(rates[i,3], row, rates[i,4], row, col=order.cols[i])
	points(rates[i,2], row, pch=19, col="grey92", cex=0.9)
	points(rates[i,2], row, pch=19, col=order.cols[i], cex=0.5)	
}

abline(h=c(4,10,13), col="white", lwd=2)
text(0.09, 17.5, "19th Century", cex=0.6, col="grey60", pos=2, font=2)
text(0.09, 12.5, "Interwar Period", cex=0.6, col="grey60", pos=2, font=2)
text(0.09, 9.5, "Cold War", cex=0.6, col="grey60", pos=2, font=2)
text(0.09, 3.5, "Post-Cold War", cex=0.6, col="grey60", pos=2, font=2)




